﻿using AutoMapper;
using Dapper;
using DocumentFormat.OpenXml.Bibliography;
using DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.Drawing.Charts;
using DocumentFormat.OpenXml.ExtendedProperties;
using DocumentFormat.OpenXml.Math;
using DocumentFormat.OpenXml.Office2010.Excel;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Wordprocessing;
using Hangfire;
using housingsafety.server.Caching.Core;
using housingsafety.server.Entities;
using housingsafety.server.Entities.DataModels;
using housingsafety.server.Helpers;
using housingsafety.server.Models;
using housingsafety.server.Repository;
using housingsafety.server.Repository.Extensions;
using housingsafety.server.Svcs;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Npgsql;
using Npoi.Mapper;
using NPOI.HPSF;
using NPOI.POIFS.Crypt.Dsig;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using Org.BouncyCastle.Asn1.Ocsp;
using Quartz.Util;
using StackExchange.Redis;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Data.SqlClient;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static housingsafety.server.Controllers.HouseMngController;
using static housingsafety.server.Controllers.HouseMngSqjzHouseController;
using static housingsafety.server.Controllers.SampleTaskController;
using static housingsafety.server.Controllers.VillageMngController;

namespace housingsafety.server.Controllers
{
    /// <summary>
    /// 大屏基本信息 展示
    /// </summary>
    [AllowAnonymous]
    public class HouseMngSqjzHouseController : BaseController<HouseMngCheckController>
    {
        public IConfiguration Configuration { get; }
        private readonly IRepository<ZmVillageFireSafety> _villageFireSafetyRepository;
        private readonly ICache _cache;
        private readonly IMapper _mapper;
        private readonly IStorageProvider _storageProvider;
        private readonly ZmDbContext _dbContext;
        private readonly IRepository<ZmUser> _userRepository;
        private readonly IRepository<ZmBuildings> _buildingsRepository;
        private readonly IRepository<ZmBuildingsLayer> _buildingsLayerRepository;
        private readonly IRepository<ZmUpdateLog> _zmupdateRepository;
        private readonly IRepository<ZmUpdateLog> _zmupdate;
        private readonly IRepository<ZmFileAttach> _fileAttachRepository;
        private readonly IRepository<ZmPlaceCommercial> _placeCommercialRepository;
        private readonly IRepository<ZmPlaceResident> _placeResidentRepository;
        private readonly IOptions<StaticDataConf> _staticVtOptions;
        private readonly IRepository<ZmDepartment> _departmentRepository;
        private readonly IRepository<RiskCensusHouseYhpc> _riskHouseRepository;
        private readonly IRepository<RiskCensusYhpcUpldFile> _riskHouseFileRepository;
        private readonly IRepository<ZmPlaceResidentCheck> _placeResidentCheckRepository;
        private readonly IRepository<ZmPlaceCommercialCheck> _placeCommercialCheckRepository;
        private readonly IRepository<ZmYtPhoto> _zmYtPhotoRepository;
        private readonly IDbTransaction _transaction;

        private readonly Microsoft.AspNetCore.Http.Features.FormOptions _defaultFormOptions = new Microsoft.AspNetCore.Http.Features.FormOptions();
        private readonly string[] _permittedExtensions = { ".mp3", ".mp4", ".jpg", ".jpeg", ".png", ".geojson", ".doc", ".docx" };

        private static object _lock = new object();

        public HouseMngSqjzHouseController(IStorageProvider storageProvider
            , IRepository<ZmDepartment> departmentRepository
            , IRepository<ZmUser> userRepository
              , IRepository<ZmVillageFireSafety> villageFireSafetyRepository
              , IRepository<ZmBuildings> buildingsRepository
            , IRepository<ZmBuildingsLayer> buildingsLayerRepository
            , IRepository<ZmFileAttach> fileAttachRepository
            , IRepository<ZmPlaceCommercial> placeCommercialRepository
            , IRepository<ZmPlaceResident> placeResidentRepository
            , IRepository<RiskCensusHouseYhpc> riskHouseRepository
            , IRepository<RiskCensusYhpcUpldFile> riskHouseFileRepository
            , IRepository<ZmPlaceResidentCheck> placeResidentCheckRepository
            , IRepository<ZmPlaceCommercialCheck> placeCommercialCheckRepository
            , IRepository<ZmYtPhoto> zmYtPhotoRepository
            , IMapper mapper, ICacheProvider cacheProvider, ZmDbContext dbContext
            , IOptions<StaticDataConf> staticVtOptions
            , ILogger<HouseMngCheckController> logger,
            IConfiguration configuration
            , IRepository<ZmSystemLog> systemLogRepository, IRepository<ZmUpdateLog> zmupdateRepository) : base(logger, systemLogRepository)
        {
            _zmupdateRepository = zmupdateRepository;
            _storageProvider = storageProvider;
            _mapper = mapper;
            _dbContext = dbContext;
            _cache = cacheProvider.CreateCache();
            _zmYtPhotoRepository = zmYtPhotoRepository;
            _userRepository = userRepository;
            _buildingsRepository = buildingsRepository;
            _buildingsLayerRepository = buildingsLayerRepository;
            _fileAttachRepository = fileAttachRepository;
            _placeCommercialRepository = placeCommercialRepository;
            _placeResidentRepository = placeResidentRepository;
            _staticVtOptions = staticVtOptions;
            _departmentRepository = departmentRepository;
            _riskHouseRepository = riskHouseRepository;
            _riskHouseFileRepository = riskHouseFileRepository;
            _placeResidentCheckRepository = placeResidentCheckRepository;
            _placeCommercialCheckRepository = placeCommercialCheckRepository;
            Configuration = configuration;
            _villageFireSafetyRepository = villageFireSafetyRepository;
            _zmupdateRepository = zmupdateRepository;

        }

        List<string> stringList = new List<string> { "栋", "栋", "处", "处" };


        public class RysstdDto
        {
            public int zcount { get; set; }

            public int wdb { get; set; }

            public int ydb { get; set; }

            public decimal dbl { get; set; }
        }



        /// <summary>
        /// 市级-人员疏散通道
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("houseszc/rysstd")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> Rysstd([FromBody] DateQuery param)
        {
            if (string.IsNullOrWhiteSpace(param.code))
            {
                param.code = "110000";
            }
            param.code = param.code.Replace("000", "");
            string sql = @" ";
            if (param.code.Length == 3)
            {
                 sql = @" select zcount,ydb,wdb,dbl dbl from  rysstd_gq_text('','','2024-01-01',now()::timestamp,'zm_pc_base')  ";
            }
            if (param.code.Length == 6)
            {
                 sql = @" select zcount,ydb,wdb,dbl dbl from  rysstd_xz_text('','','2024-01-01',now()::timestamp,'zm_pc_base',@code)  ";
            }
            if (param.code.Length == 9)
            {
                 sql = @" select zcount,ydb,wdb,dbl dbl from  rysstd_cj_text('','','2024-01-01',now()::timestamp,'zm_pc_base',@code)  ";
            }
            if (param.code.Length == 12)
            {
                 sql = @" select zcount,ydb,wdb,dbl dbl from  rysstd_cj_text('','','2024-01-01',now()::timestamp,'zm_pc_base',@code)  ";
            }

            var msg = _dbContext.Database.GetDbConnection().Query<RysstdDto>(sql, new { @code = "%" + param.code + "%" }).FirstOrDefault();
            var tb = new List<object>();
            tb.Add(new { value = msg.dbl, name = "已达标" });
            tb.Add(new { value = 100 - msg.dbl, name = "未达标" });
            return new RespRet<object>(true, "Ok", new { msg = msg, tb = tb });
        }




        /// <summary>
        /// 已达标未达标饼状图二级页面，按达标率倒序排
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("houseszc/dblbar")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]

        public RespRet<object> Dblbar([FromBody] DateQuery param)
        {
            try
            {

                if (string.IsNullOrWhiteSpace(param.code))
                {
                    param.code = "110000";
                }
                param.code = param.code.Replace("000", "");
                string sql = @" ";
                if (param.code.Length == 3)
                {
                    sql = @" select zcount,ydb,wdb,dbl||'%' dbl,name,code from  dblbar_gq_text('','','2024-01-01',now()::timestamp,'zm_pc_base')  ";
                }
                if (param.code.Length == 6)
                {
                    sql = @" select zcount,ydb,wdb,dbl||'%' dbl,name,code,name town,code towndm from  dblbar_xz_text('','','2024-01-01',now()::timestamp,'zm_pc_base',@code)  ";
                }
                if (param.code.Length == 9)
                {
                    sql = @" select zcount,ydb,wdb,dbl||'%' dbl,name,code,name village,code villagedm from  dblbar_cj_text('','','2024-01-01',now()::timestamp,'zm_pc_base',@code)  ";
                }
                if (param.code.Length == 12)
                {
                    sql = @" select zcount,ydb,wdb,dbl||'%' dbl,name,code,name village,code villagedm from  dblbar_cj_text('','','2024-01-01',now()::timestamp,'zm_pc_base',@code)  ";
                }

                var data = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + param.code + "%"}).ToList();
                var res = new RespRet<object>(true, "Ok", data);
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                res.Msg = "系统异常";
                return res;
            }
        }




        /// <summary>
        /// 村级检查统计-人员疏散通道
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescjjc/rysstd")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> CjjcRysstd([FromBody] DateQuery param)
        {

            if (string.IsNullOrWhiteSpace(param.code))
            {
                param.code = "110000";
            }
            param.code = param.code.Replace("000", "");
            string sql = @" ";
            if (param.code.Length == 3 )
            {
                sql = @" select  zcount,(zcount-ydb)wdb,ydb,case when  round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (select case when  sum(ssltdbfjsl) is null then 0 else  sum(ssltdbfjsl) end ydb ,sum(zcount) zcount  from  (	SELECT 
    t1.xzq,
    t1.xzqdm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count(1) AS zcount,
            zm_buildings.xzq,
            zm_buildings.xzqdm 
        FROM (
            SELECT t1.*, t2.fwzt
            FROM zm_buildings t1 
            LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh::text = t2.fwbh::text
        ) AS zm_buildings  
        WHERE fwzt IS NULL  and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')
        GROUP BY zm_buildings.xzqdm, zm_buildings.xzq
        ORDER BY zm_buildings.xzqdm
    ) AS t1
    LEFT JOIN (
        SELECT 
                count(1) ssltdbfjsl,xzqdm,xzq
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on, t1.xzq,jsrysstd, t1.xzqdm,t1.jzxyssltsl, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_cj t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE    sfyzc='是'  
        ) AS zm_pc_base where row_num =1  AND fwzt IS NULL  and jsrysstd='是'and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY xzqdm,xzq

    ) AS t2 ON t1.xzqdm::text = t2.xzqdm::text
) 
) zcqkgqview ) a
   ";
            }
            if (param.code.Length == 6)
            {
                sql = @" select  zcount,(zcount-ydb)wdb,ydb,case when  round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (select case when  sum(ssltdbfjsl) is null then 0 else  sum(ssltdbfjsl) end ydb ,sum(zcount) zcount  from  (	SELECT 
    t1.towndm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count(1) AS zcount,
            zm_buildings.towndm  
        FROM (
            SELECT t1.*, t2.fwzt
            FROM zm_buildings t1 
            LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh::text = t2.fwbh::text
        ) AS zm_buildings  
        WHERE fwzt IS NULL  and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')
        GROUP BY zm_buildings.towndm
        ORDER BY zm_buildings.towndm
    ) AS t1
    LEFT JOIN (
        SELECT 
                count(1) ssltdbfjsl,town
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,jsrysstd,t1.town, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_cj t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE  sfyzc='是'  
        ) AS zm_pc_base where row_num =1  AND fwzt IS NULL  and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY town
    ) AS t2 ON t1.towndm::text = t2.town::text
) 
 where t1.towndm like @code
) zcqkgqview ) a    
            ";
            }
            if (param.code.Length == 9|| param.code.Length == 12)
            {
                sql = @" select  zcount,(zcount-ydb)wdb,ydb,case when  round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (select case when  sum(ssltdbfjsl) is null then 0 else  sum(ssltdbfjsl) end ydb ,sum(zcount) zcount from  (	SELECT 
    t1.villagedm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count(1) AS zcount,
            zm_buildings.villagedm  
        FROM (
            SELECT t1.*, t2.fwzt
            FROM zm_buildings t1 
            LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh::text = t2.fwbh::text
        ) AS zm_buildings  
        WHERE fwzt IS NULL  and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')
        GROUP BY zm_buildings.villagedm
        ORDER BY zm_buildings.villagedm
    ) AS t1
    LEFT JOIN (
         SELECT 
                count(1) ssltdbfjsl,village
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,jsrysstd,t1.village, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_cj t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE sfyzc='是'  
        ) AS zm_pc_base where row_num =1  AND fwzt IS NULL  and jsrysstd='是'and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY village
    ) AS t2 ON t1.villagedm::text = t2.village::text
) 
 where t1.villagedm like @code
) zcqkgqview ) a  
     ";
            }
            var msg = _dbContext.Database.GetDbConnection().Query<RysstdDto>(sql, new { @code = "%" + param.code + "%"}).FirstOrDefault();
            var tb = new List<object>();
            tb.Add(new { value = msg.dbl, name = "已达标" });
            tb.Add(new { value = 100 - msg.dbl, name = "未达标" });
            return new RespRet<object>(true, "Ok", new { msg = msg, tb = tb });
        }


        /// <summary>
        /// 街镇核查统计-人员疏散通道
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housesjzhc/rysstd")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> JzhcRysstd([FromBody] DateQuery param)
        {

            if (string.IsNullOrWhiteSpace(param.code))
            {
                param.code = "110000";
            }
            param.code = param.code.Replace("000", "");
            
            string sql = @" ";
            if (param.code.Length == 3 )
            {
                sql = @" select  COALESCE(zcount,0) zcount, COALESCE((zcount-ydb),0) wdb,ydb,case when  round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (select case when  sum(ssltdbfjsl) is null then 0 else  sum(ssltdbfjsl) end ydb ,sum(zcount) zcount  from  (	SELECT 
    t1.xzq,
    t1.xzqdm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count(1) AS zcount,
            zm_buildings.xzq,
            zm_buildings.xzqdm 
        FROM ( select xzq,xzqdm from zm_buildings where fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='4' and fwzt is null ) and fwbh not in ( select distinct fwbh from zm_pc_base_cj where   fwzt is not null )  )zm_buildings  
			 
        GROUP BY zm_buildings.xzqdm, zm_buildings.xzq
        ORDER BY zm_buildings.xzqdm
    ) AS t1
    LEFT JOIN (
        SELECT 
                count(1) ssltdbfjsl,xzqdm,xzq
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on, t1.xzq,jsrysstd, t1.xzqdm,t1.jzxyssltsl, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE  yhly='4'  and sfyzc='是'  
        ) AS zm_pc_base where row_num =1   and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY xzqdm,xzq
    ) AS t2 ON t1.xzqdm::text = t2.xzqdm::text 	
) 
) zcqkgqview ) a ";
            }
            if (param.code.Length == 6)
            {
                sql = @" select  COALESCE(zcount,0) zcount, COALESCE((zcount-ydb),0) wdb,ydb,case when  round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (select case when  sum(ssltdbfjsl) is null then 0 else  sum(ssltdbfjsl) end ydb ,sum(zcount) zcount  from  (	SELECT 
    t1.towndm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count(1) AS zcount,
            zm_buildings.towndm  
        FROM   ( select towndm,xzqdm from zm_buildings where fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='4' and fwzt is  null  ) and fwbh not in ( select distinct fwbh from zm_pc_base_cj where   fwzt is not null )  ) zm_buildings  
        GROUP BY zm_buildings.towndm
        ORDER BY zm_buildings.towndm
    ) AS t1
    LEFT JOIN (
        SELECT 
                count(1) ssltdbfjsl,town
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,jsrysstd,t1.town, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE yhly='4'  and sfyzc='是'  
        ) AS zm_pc_base where row_num =1  and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY town
    ) AS t2 ON t1.towndm::text = t2.town::text
) 
 where t1.towndm like @code
) zcqkgqview ) a    ";
            }
            if (param.code.Length == 12|| param.code.Length == 9)
            {
                sql = @" select  COALESCE(zcount,0) zcount, COALESCE((zcount-ydb),0) wdb,ydb,case when  round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (select case when  sum(ssltdbfjsl) is null then 0 else  sum(ssltdbfjsl) end ydb ,sum(zcount) zcount from  (	SELECT 
    t1.villagedm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count(1) AS zcount,
            zm_buildings.villagedm  
        FROM  ( select villagedm from zm_buildings where fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='4' and fwzt is  null  ) and fwbh not in ( select distinct fwbh from zm_pc_base_cj where   fwzt is not null )  )zm_buildings   
        GROUP BY zm_buildings.villagedm
        ORDER BY zm_buildings.villagedm
    ) AS t1
    LEFT JOIN (
         SELECT 
                count(1) ssltdbfjsl,village
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,jsrysstd,t1.village, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE yhly='4' and sfyzc='是'  
        ) AS zm_pc_base where row_num =1  and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY village
    ) AS t2 ON t1.villagedm::text = t2.village::text
) 
 where t1.villagedm like @code
) zcqkgqview ) a   ";
            }
            var msg = _dbContext.Database.GetDbConnection().Query<RysstdDto>(sql, new { @code = "%" + param.code + "%"}).FirstOrDefault();
            var tb = new List<object>();
            tb.Add(new { value = msg.dbl, name = "已达标" });
            tb.Add(new { value = 100 - msg.dbl, name = "未达标" });
            return new RespRet<object>(true, "Ok", new { msg = msg, tb = tb });
        }





        /// <summary>
        /// 区级核查统计-人员疏散通道
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housesqjhc/rysstd")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> QjhcRysstd([FromBody] DateQuery param)
        {

            if (string.IsNullOrWhiteSpace(param.code))
            {
                param.code = "110000";
            }
            param.code = param.code.Replace("000", "");
            string sql = @" ";
            if (param.code.Length == 3 )
            {
                sql = @" select  COALESCE(zcount,0) zcount, COALESCE((zcount-ydb),0) wdb,ydb,case when  round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (select case when  sum(ssltdbfjsl) is null then 0 else  sum(ssltdbfjsl) end ydb ,sum(zcount) zcount  from  (	SELECT 
    t1.xzq,
    t1.xzqdm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count(1) AS zcount,
            zm_buildings.xzq,
            zm_buildings.xzqdm 
        FROM ( select xzq,xzqdm from zm_buildings where fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='3' and fwzt is  null   ) and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') )zm_buildings  
			 
        GROUP BY zm_buildings.xzqdm, zm_buildings.xzq
        ORDER BY zm_buildings.xzqdm
    ) AS t1
    LEFT JOIN (
        SELECT 
                count(1) ssltdbfjsl,xzqdm,xzq
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on, t1.xzq,jsrysstd, t1.xzqdm,t1.jzxyssltsl, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE  yhly='3' and sfyzc='是'   
        ) AS zm_pc_base where row_num =1   and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY xzqdm,xzq 
    ) AS t2 ON t1.xzqdm::text = t2.xzqdm::text 	
) 
) zcqkgqview ) a ";
            }
            if (param.code.Length == 6)
            {
                sql = @" select  COALESCE(zcount,0) zcount, COALESCE((zcount-ydb),0) wdb,ydb,case when  round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (select case when  sum(ssltdbfjsl) is null then 0 else  sum(ssltdbfjsl) end ydb ,sum(zcount) zcount  from  (	SELECT 
    t1.towndm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count(1) AS zcount,
            zm_buildings.towndm  
        FROM   ( select towndm,xzqdm from zm_buildings where fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='3' and fwzt is  null  ) and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')  ) zm_buildings  
        GROUP BY zm_buildings.towndm
        ORDER BY zm_buildings.towndm
    ) AS t1
    LEFT JOIN (
        SELECT 
                count(1) ssltdbfjsl,town
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,jsrysstd,t1.town, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE yhly='3'  and sfyzc='是'  
        ) AS zm_pc_base where row_num =1  and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY town
    ) AS t2 ON t1.towndm::text = t2.town::text
) 
 where t1.towndm like @code
) zcqkgqview ) a    ";
            }
            if (param.code.Length == 12|| param.code.Length == 9)
            {
                sql = @" select  COALESCE(zcount,0) zcount, COALESCE((zcount-ydb),0) wdb,ydb,case when  round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (select case when  sum(ssltdbfjsl) is null then 0 else  sum(ssltdbfjsl) end ydb ,sum(zcount) zcount from  (	SELECT 
    t1.villagedm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count(1) AS zcount,
            zm_buildings.villagedm  
        FROM  ( select villagedm from zm_buildings where fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='3'and fwzt is  null   ) and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')  )zm_buildings   
        GROUP BY zm_buildings.villagedm
        ORDER BY zm_buildings.villagedm
    ) AS t1
    LEFT JOIN (
         SELECT 
                count(1) ssltdbfjsl,village
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,jsrysstd,t1.village, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE yhly='3' and sfyzc='是'  
        ) AS zm_pc_base where row_num =1  and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY village
    ) AS t2 ON t1.villagedm::text = t2.village::text
) 
 where t1.villagedm like @code
) zcqkgqview ) a   ";
            }
            var msg = _dbContext.Database.GetDbConnection().Query<RysstdDto>(sql, new { @code = "%" + param.code + "%" }).FirstOrDefault();
            var tb = new List<object>();
            tb.Add(new { value = msg.dbl, name = "已达标" });
            tb.Add(new { value = 100 - msg.dbl, name = "未达标" });
            return new RespRet<object>(true, "Ok", new { msg = msg, tb = tb });
        }


        /// <summary>
        /// 市级核查统计-人员疏散通道
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housessjhc/rysstd")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> SjhcRysstd([FromBody] DateQuery param)
        {

            if (string.IsNullOrWhiteSpace(param.code))
            {
                param.code = "110000";
            }
            param.code = param.code.Replace("000", "");
            string sql = @" ";
            if (param.code.Length == 3 )
            {
                sql = @" select  COALESCE(zcount,0) zcount, COALESCE((zcount-ydb),0) wdb,ydb,case when  round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (select case when  sum(ssltdbfjsl) is null then 0 else  sum(ssltdbfjsl) end ydb ,sum(zcount) zcount  from  (	SELECT 
    t1.xzq,
    t1.xzqdm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count(1) AS zcount,
            zm_buildings.xzq,
            zm_buildings.xzqdm 
        FROM ( select xzq,xzqdm from zm_buildings where fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='2' and fwzt is  null ) and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')  )zm_buildings  
			 
        GROUP BY zm_buildings.xzqdm, zm_buildings.xzq
        ORDER BY zm_buildings.xzqdm
    ) AS t1
    LEFT JOIN (
        SELECT 
                count(1) ssltdbfjsl,xzqdm,xzq
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on, t1.xzq,jsrysstd, t1.xzqdm,t1.jzxyssltsl, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE  yhly='2' and sfyzc='是'  
        ) AS zm_pc_base where row_num =1   and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY xzqdm,xzq
    ) AS t2 ON t1.xzqdm::text = t2.xzqdm::text 	
) 
) zcqkgqview ) a ";
            }
            if (param.code.Length == 6)
            {
                sql = @" select  COALESCE(zcount,0) zcount, COALESCE((zcount-ydb),0) wdb,ydb,case when  round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (select case when  sum(ssltdbfjsl) is null then 0 else  sum(ssltdbfjsl) end ydb ,sum(zcount) zcount  from  (	SELECT 
    t1.towndm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count(1) AS zcount,
            zm_buildings.towndm  
        FROM   ( select towndm,xzqdm from zm_buildings where fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='2' and fwzt is  null ) and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') ) zm_buildings  
        GROUP BY zm_buildings.towndm
        ORDER BY zm_buildings.towndm
    ) AS t1
    LEFT JOIN (
        SELECT 
                count(1) ssltdbfjsl,town
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,jsrysstd,t1.town, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE yhly='2'  and sfyzc='是'  
        ) AS zm_pc_base where row_num =1  and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY town
    ) AS t2 ON t1.towndm::text = t2.town::text
) 
 where t1.towndm like @code
) zcqkgqview ) a    ";
            }
            if (param.code.Length == 12 || param.code.Length == 9)
            {
                sql = @" select  COALESCE(zcount,0) zcount, COALESCE((zcount-ydb),0) wdb,ydb,case when  round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (select case when  sum(ssltdbfjsl) is null then 0 else  sum(ssltdbfjsl) end ydb ,sum(zcount) zcount from  (	SELECT 
    t1.villagedm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count(1) AS zcount,
            zm_buildings.villagedm  
        FROM  ( select villagedm from zm_buildings where fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='2' and fwzt is  null  ) and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')  )zm_buildings   
        GROUP BY zm_buildings.villagedm
        ORDER BY zm_buildings.villagedm
    ) AS t1
    LEFT JOIN (
         SELECT 
                count(1) ssltdbfjsl,village
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,jsrysstd,t1.village, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE yhly='2' AND  sfyzc='是'  
        ) AS zm_pc_base where row_num =1  and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY village
    ) AS t2 ON t1.villagedm::text = t2.village::text
) 
 where t1.villagedm like @code
) zcqkgqview ) a   ";
            }
            var msg = _dbContext.Database.GetDbConnection().Query<RysstdDto>(sql, new { @code = "%" + param.code + "%" }).FirstOrDefault();
            var tb = new List<object>();
            tb.Add(new { value = msg.dbl, name = "已达标" });
            tb.Add(new { value = 100 - msg.dbl, name = "未达标" });
            return new RespRet<object>(true, "Ok", new { msg = msg, tb = tb });
        }





        /// <summary>
        /// 达标率排名村级检查
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescjjc/dblbar")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]

        public RespRet<object> CjjcDblbar([FromBody] DateQuery param)
        {
            try
            {

                if (string.IsNullOrWhiteSpace(param.code))
                {
                    param.code = "110000";
                }
                param.code = param.code.Replace("000", "");
                string sql = @" ";
                if (param.code.Length == 3 )
                {
                    sql = @"  
select   zcount,(zcount-ydb) wdb,ydb,name,code,CASE WHEN dbl > 100 THEN 100.00  when dbl is null then 0.00 ELSE dbl END dbl  from  (select ydb,zcount,xzq name,xzqdm code,case when  round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (select max(jbxxgqview.xzq) xzq,jbxxgqview.xzqdm,sum(jbxxgqview.zcount) zcount,sum(ssltdbfjsl) ydb   from (SELECT 
    t1.xzq,
    t1.xzqdm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
		COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count(1) AS zcount,
            zm_buildings.xzq,
            zm_buildings.xzqdm 
        FROM (
            SELECT t1.*, t2.fwzt
            FROM zm_buildings t1 
            LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh::text = t2.fwbh::text
        ) AS zm_buildings  
        WHERE fwzt IS NULL and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')
        GROUP BY zm_buildings.xzqdm, zm_buildings.xzq
        ORDER BY zm_buildings.xzqdm
    ) AS t1
    LEFT JOIN (
       SELECT 
                count(1) ssltdbfjsl,xzqdm,xzq
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on, t1.xzq,jsrysstd, t1.xzqdm,t1.jzxyssltsl, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_cj t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE  sfyzc='是'  
        ) AS zm_pc_base where row_num =1  AND fwzt IS NULL  and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')  GROUP BY xzqdm,xzq
    ) AS t2 ON t1.xzqdm::text = t2.xzqdm::text
) 
) jbxxgqview  GROUP BY xzqdm ) a ORDER BY dbl desc,xzqdm )a 	
	  ";
                }
                if (param.code.Length == 6)
                {
                    sql = @"  
  select  xzqdm,xzq,zcount,(zcount-ydb) wdb,ydb,name,code,CASE WHEN dbl > 100 THEN 100.00  when dbl is null then 0.00 ELSE dbl END dbl  from  (select COALESCE(ssltdbfjsl,0) ydb,zcount,xzqdm,xzq,town name,towndm code,case when  round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (SELECT 
    t1.xzq,
    t1.xzqdm,
		t1.town,
		t1.towndm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count(1) AS zcount,
            max(zm_buildings.xzq)xzq,
						 max(zm_buildings.xzqdm)xzqdm,
						  max(zm_buildings.town)town,
            zm_buildings.towndm 
        FROM (
            SELECT t1.*, t2.fwzt
            FROM zm_buildings t1 
            LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh::text = t2.fwbh::text
        ) AS zm_buildings  
        WHERE fwzt IS NULL and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')
        GROUP BY zm_buildings.towndm
        ORDER BY zm_buildings.towndm
    ) AS t1
    LEFT JOIN (
        SELECT 
                count(1) ssltdbfjsl,town
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.town,jsrysstd, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_cj t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE  sfyzc='是'  
        ) AS zm_pc_base where row_num =1  AND fwzt IS NULL  and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY town
    ) AS t2 ON t1.towndm::text = t2.town::text
) 
 where t1.towndm like @code
) jbxxgqview   ) a ORDER BY dbl desc,code ";
                }
                if (param.code.Length == 9)
                {
                    sql = @"select zcount,(zcount-ydb) wdb,town,towndm,xzqdm,xzq,zcount,ydb,name,code,CASE WHEN dbl > 100 THEN 100.00  when dbl is null then 0.00 ELSE dbl END dbl  from  (select COALESCE(ssltdbfjsl,0) ydb,zcount,xzqdm,xzq,village name,villagedm code,town,towndm,case when  round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (SELECT 
		t1.town,
		t1.towndm,
    t1.xzq,
    t1.xzqdm,
		t1.village,
		t1.villagedm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count(1) AS zcount,
						max(zm_buildings.town) town,
						max(zm_buildings.towndm) towndm,
            max(zm_buildings.xzq)xzq,
						 max(zm_buildings.xzqdm)xzqdm,
						  max(zm_buildings.village)village,
            zm_buildings.villagedm 
        FROM (
            SELECT t1.*, t2.fwzt
            FROM zm_buildings t1 
            LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh::text = t2.fwbh::text
        ) AS zm_buildings  
        WHERE fwzt IS NULL and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')
        GROUP BY zm_buildings.villagedm
        ORDER BY zm_buildings.villagedm
    ) AS t1
    LEFT JOIN (
         SELECT 
                count(1) ssltdbfjsl,village
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.village,jsrysstd, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_cj t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE  sfyzc='是'  
        ) AS zm_pc_base where row_num =1  AND fwzt IS NULL  and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY village
    ) AS t2 ON t1.villagedm::text = t2.village::text
) 
 where t1.villagedm like @code
) jbxxgqview   ) a ORDER BY dbl desc,code 
 ";
                }
                if (param.code.Length == 12)
                {
                    sql = @" select zcount,(zcount-ydb) wdb,town,towndm,xzqdm,xzq,zcount,ydb,name,code,CASE WHEN dbl > 100 THEN 100.00  when dbl is null then 0.00 ELSE dbl END dbl  from  (select COALESCE(ssltdbfjsl,0) ydb,zcount,xzqdm,xzq,village name,villagedm code,town,towndm,case when  round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (SELECT 
		t1.town,
		t1.towndm,
    t1.xzq,
    t1.xzqdm,
		t1.village,
		t1.villagedm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count(1) AS zcount,
						max(zm_buildings.town) town,
						max(zm_buildings.towndm) towndm,
            max(zm_buildings.xzq)xzq,
						 max(zm_buildings.xzqdm)xzqdm,
						  max(zm_buildings.village)village,
            zm_buildings.villagedm 
        FROM (
            SELECT t1.*, t2.fwzt
            FROM zm_buildings t1 
            LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh::text = t2.fwbh::text
        ) AS zm_buildings  
        WHERE fwzt IS NULL and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')
        GROUP BY zm_buildings.villagedm
        ORDER BY zm_buildings.villagedm
    ) AS t1
    LEFT JOIN (
         SELECT 
                count(1) ssltdbfjsl,village
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.village,jsrysstd, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_cj t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE  sfyzc='是'  
        ) AS zm_pc_base where row_num =1  AND fwzt IS NULL  and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY village
    ) AS t2 ON t1.villagedm::text = t2.village::text
) 
 where t1.villagedm like @code
) jbxxgqview   ) a ORDER BY dbl desc,code 
 ";
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + param.code + "%" }).ToList();
                var res = new RespRet<object>(true, "Ok", data);
                
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                res.Msg = "系统异常";
                return res;
            }
        }





        /// <summary>
        /// 街镇达标率排名
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housesjzhc/dblbar")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]

        public RespRet<object> JzDblbar([FromBody] DateQuery param)
        {
            try
            {

                if (string.IsNullOrWhiteSpace(param.code))
                {
                    param.code = "110000";
                }
                param.code = param.code.Replace("000", "");
                string sql = @" ";
                if (param.code.Length == 3 )
                {
                    sql = @"  
select   zcount,(zcount-ydb) wdb,sjfwzs,ydb,name,code,CASE WHEN dbl > 100 THEN 100.00  when dbl is null then 0.00 ELSE dbl END dbl  from  (select ydb,zcount,sjfwzs,xzq name,xzqdm code,case when  round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (select max(jbxxgqview.xzq) xzq,jbxxgqview.xzqdm,sum(jbxxgqview.zcount) zcount,sum(ssltdbfjsl) ydb ,sum(sjfwzs) sjfwzs   from (SELECT 
    t1.xzq,
    t1.xzqdm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
   COALESCE((t1.sjfwzs)::numeric, (0)::numeric) AS sjfwzs,
		COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
             count(1) sjfwzs,
             count( case when fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='4' and fwzt is null ) and fwbh not in ( select distinct fwbh from zm_pc_base_cj where   fwzt is not null ) then fwbh end ) AS zcount,
            zm_buildings.xzq,
            zm_buildings.xzqdm 
        FROM zm_buildings   
        GROUP BY zm_buildings.xzqdm, zm_buildings.xzq
        ORDER BY zm_buildings.xzqdm
    ) AS t1
    LEFT JOIN (
       SELECT 
                count(1) ssltdbfjsl,xzqdm,xzq
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on, t1.xzq,jsrysstd, t1.xzqdm,t1.jzxyssltsl, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE  yhly='4'and sfyzc='是'  
        ) AS zm_pc_base where row_num =1   and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY xzqdm,xzq
    ) AS t2 ON t1.xzqdm::text = t2.xzqdm::text
) 
) jbxxgqview  GROUP BY xzqdm ) a ORDER BY dbl desc,xzqdm )a 	
	  ";
                }
                if (param.code.Length == 6)
                {
                    sql = @"  
  select  xzqdm,xzq,sjfwzs,zcount,(zcount-ydb) wdb,ydb,name,code,CASE WHEN dbl > 100 THEN 100.00  when dbl is null then 0.00 ELSE dbl END dbl  from  (select COALESCE(ssltdbfjsl,0) ydb,sjfwzs,zcount,xzqdm,xzq,town name,towndm code,case when  round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (SELECT 
    t1.xzq,
    t1.xzqdm,
		t1.town,
		t1.towndm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
   COALESCE((t1.sjfwzs)::numeric, (0)::numeric) AS sjfwzs,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count( case when fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='4' and fwzt is null) and fwbh not in ( select distinct fwbh from zm_pc_base_cj where   fwzt is not null ) then fwbh end ) AS zcount,
            count(1)   sjfwzs,
                          max(zm_buildings.xzq)xzq,
						 max(zm_buildings.xzqdm)xzqdm,
						  max(zm_buildings.town)town,
            zm_buildings.towndm 
        FROM  zm_buildings
        GROUP BY zm_buildings.towndm
        ORDER BY zm_buildings.towndm
    ) AS t1
    LEFT JOIN (
        SELECT 
                count(1) ssltdbfjsl,town
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.town,jsrysstd, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE yhly='4' and sfyzc='是'  
        ) AS zm_pc_base where row_num =1  and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY town
    ) AS t2 ON t1.towndm::text = t2.town::text
) 
 where t1.towndm like @code
) jbxxgqview   ) a ORDER BY dbl desc,code ";
                }
                if (param.code.Length == 9)
                {
                    sql = @"select zcount,sjfwzs,(zcount-ydb) wdb,town,towndm,xzqdm,xzq,zcount,ydb,name,code,CASE WHEN dbl > 100 THEN 100.00  when dbl is null then 0.00 ELSE dbl END dbl  from  (select COALESCE(ssltdbfjsl,0) ydb,sjfwzs,zcount,xzqdm,xzq,village name,villagedm code,town,towndm,case when  round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (SELECT 
		t1.town,
		t1.towndm,
    t1.xzq,
    t1.xzqdm,
		t1.village,
		t1.villagedm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE((t1.sjfwzs)::numeric, (0)::numeric) AS sjfwzs,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
             count( case when fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='4' and fwzt is null ) and fwbh not in ( select distinct fwbh from zm_pc_base_cj where   fwzt is not null ) then fwbh end ) AS zcount,
				        count(1) sjfwzs,
                        max(zm_buildings.town) town,
						max(zm_buildings.towndm) towndm,
            max(zm_buildings.xzq)xzq,
						 max(zm_buildings.xzqdm)xzqdm,
						  max(zm_buildings.village)village,
            zm_buildings.villagedm 
        FROM zm_buildings 
        GROUP BY zm_buildings.villagedm
        ORDER BY zm_buildings.villagedm
    ) AS t1
    LEFT JOIN (
         SELECT 
                count(1) ssltdbfjsl,village
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.village,jsrysstd, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE yhly='4'  and sfyzc='是'  
        ) AS zm_pc_base where row_num =1   and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY village
    ) AS t2 ON t1.villagedm::text = t2.village::text
) 
 where t1.villagedm like @code
) jbxxgqview   ) a ORDER BY dbl desc,code 
 ";
                }
                if (param.code.Length == 12)
                {
                    sql = @" select zcount,(zcount-ydb) wdb,sjfwzs,town,towndm,xzqdm,xzq,zcount,ydb,name,code,CASE WHEN dbl > 100 THEN 100.00  when dbl is null then 0.00 ELSE dbl END dbl  from  (select COALESCE(ssltdbfjsl,0) ydb,sjfwzs,zcount,xzqdm,xzq,village name,villagedm code,town,towndm,case when  round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (SELECT 
		t1.town,
		t1.towndm,
    t1.xzq,
    t1.xzqdm,
		t1.village,
		t1.villagedm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
 COALESCE((t1.sjfwzs)::numeric, (0)::numeric) AS sjfwzs,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
             count( case when fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='4' and fwzt is null ) and fwbh not in ( select distinct fwbh from zm_pc_base_cj where   fwzt is not null ) then fwbh end ) AS zcount,
				count(1) sjfwzs,,
                        max(zm_buildings.town) town,
						max(zm_buildings.towndm) towndm,
            max(zm_buildings.xzq)xzq,
						 max(zm_buildings.xzqdm)xzqdm,
						  max(zm_buildings.village)village,
            zm_buildings.villagedm 
        FROM zm_buildings 
        GROUP BY zm_buildings.villagedm
        ORDER BY zm_buildings.villagedm
    ) AS t1
    LEFT JOIN (
         SELECT 
                count(1) ssltdbfjsl,village
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.village,jsrysstd, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE yhly='4'  and sfyzc='是'  
        ) AS zm_pc_base where row_num =1  and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY village
    ) AS t2 ON t1.villagedm::text = t2.village::text
) 
 where t1.villagedm like @code
) jbxxgqview   ) a ORDER BY dbl desc,code 
 ";
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + param.code + "%" }).ToList();
                var res = new RespRet<object>(true, "Ok", data);
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                res.Msg = "系统异常";
                return res;
            }
        }











        /// <summary>
        /// 区达标率排名
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housesqjhc/dblbar")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]

        public RespRet<object> QjDblbar([FromBody] DateQuery param)
        {
            try
            {

                if (string.IsNullOrWhiteSpace(param.code))
                {
                    param.code = "110000";
                }
                param.code = param.code.Replace("000", "");

                string sql = @" ";
                if (param.code.Length == 3 )
                {
                    sql = @"  
select   zcount,(zcount-ydb) wdb,sjfwzs,ydb,name,code,CASE WHEN dbl > 100 THEN 100.00  when dbl is null then 0.00 ELSE dbl END dbl  from  (select ydb,zcount,sjfwzs,xzq name,xzqdm code,case when  round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (select max(jbxxgqview.xzq) xzq,jbxxgqview.xzqdm,sum(jbxxgqview.zcount) zcount,sum(ssltdbfjsl) ydb ,sum(sjfwzs) sjfwzs   from (SELECT 
    t1.xzq,
    t1.xzqdm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
   COALESCE((t1.sjfwzs)::numeric, (0)::numeric) AS sjfwzs,
		COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
             count(1) sjfwzs,
             count( case when fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='3' and fwzt is null ) and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') then fwbh end ) AS zcount,
            zm_buildings.xzq,
            zm_buildings.xzqdm 
        FROM zm_buildings   
        GROUP BY zm_buildings.xzqdm, zm_buildings.xzq
        ORDER BY zm_buildings.xzqdm
    ) AS t1
    LEFT JOIN (
       SELECT 
                count(1) ssltdbfjsl,xzqdm,xzq
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on, t1.xzq,jsrysstd, t1.xzqdm,t1.jzxyssltsl, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE  yhly='3' and sfyzc='是'  
        ) AS zm_pc_base where row_num =1   and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY xzqdm,xzq
    ) AS t2 ON t1.xzqdm::text = t2.xzqdm::text
) 
) jbxxgqview  GROUP BY xzqdm ) a ORDER BY dbl desc,xzqdm )a 	
	  ";
                }
                if (param.code.Length == 6)
                {
                    sql = @"  
  select  xzqdm,xzq,sjfwzs,zcount,(zcount-ydb) wdb,ydb,name,code,CASE WHEN dbl > 100 THEN 100.00  when dbl is null then 0.00 ELSE dbl END dbl  from  (select COALESCE(ssltdbfjsl,0) ydb,sjfwzs,zcount,xzqdm,xzq,town name,towndm code,case when  round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (SELECT 
    t1.xzq,
    t1.xzqdm,
		t1.town,
		t1.towndm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
   COALESCE((t1.sjfwzs)::numeric, (0)::numeric) AS sjfwzs,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count( case when fwbh in  (select distinct fwbh from zm_pc_base_sqjz where yhly='3' and fwzt is null ) and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') then fwbh end ) AS zcount,
            count(1)   sjfwzs,
                          max(zm_buildings.xzq)xzq,
						 max(zm_buildings.xzqdm)xzqdm,
						  max(zm_buildings.town)town,
            zm_buildings.towndm 
        FROM  zm_buildings
        GROUP BY zm_buildings.towndm
        ORDER BY zm_buildings.towndm
    ) AS t1
    LEFT JOIN (
        SELECT 
                count(1) ssltdbfjsl,town
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.town,jsrysstd, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE yhly='3'  and sfyzc='是'  
        ) AS zm_pc_base where row_num =1  and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY town
    ) AS t2 ON t1.towndm::text = t2.town::text
) 
 where t1.towndm like @code
) jbxxgqview   ) a ORDER BY dbl desc,code ";
                }
                if (param.code.Length == 9)
                {
                    sql = @"select zcount,sjfwzs,(zcount-ydb) wdb,town,towndm,xzqdm,xzq,zcount,ydb,name,code,CASE WHEN dbl > 100 THEN 100.00  when dbl is null then 0.00 ELSE dbl END dbl  from  (select COALESCE(ssltdbfjsl,0) ydb,sjfwzs,zcount,xzqdm,xzq,village name,villagedm code,town,towndm,case when  round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (SELECT 
		t1.town,
		t1.towndm,
    t1.xzq,
    t1.xzqdm,
		t1.village,
		t1.villagedm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE((t1.sjfwzs)::numeric, (0)::numeric) AS sjfwzs,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
             count( case when fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='3' and fwzt is null ) and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') then fwbh end ) AS zcount,
				        count(1) sjfwzs,
                        max(zm_buildings.town) town,
						max(zm_buildings.towndm) towndm,
            max(zm_buildings.xzq)xzq,
						 max(zm_buildings.xzqdm)xzqdm,
						  max(zm_buildings.village)village,
            zm_buildings.villagedm 
        FROM zm_buildings 
        GROUP BY zm_buildings.villagedm
        ORDER BY zm_buildings.villagedm
    ) AS t1
    LEFT JOIN (
         SELECT 
                count(1) ssltdbfjsl,village
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.village,jsrysstd, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE yhly='3' and  sfyzc='是'  
        ) AS zm_pc_base where row_num =1   and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY village
    ) AS t2 ON t1.villagedm::text = t2.village::text
) 
 where t1.villagedm like @code
) jbxxgqview   ) a ORDER BY dbl desc,code 
 ";
                }
                if (param.code.Length == 12)
                {
                    sql = @" select zcount,(zcount-ydb) wdb,sjfwzs,town,towndm,xzqdm,xzq,zcount,ydb,name,code,CASE WHEN dbl > 100 THEN 100.00  when dbl is null then 0.00 ELSE dbl END dbl  from  (select COALESCE(ssltdbfjsl,0) ydb,sjfwzs,zcount,xzqdm,xzq,village name,villagedm code,town,towndm,case when  round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (SELECT 
		t1.town,
		t1.towndm,
    t1.xzq,
    t1.xzqdm,
		t1.village,
		t1.villagedm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
 COALESCE((t1.sjfwzs)::numeric, (0)::numeric) AS sjfwzs,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
             count( case when fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='3' and fwzt is null ) and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') then fwbh end ) AS zcount,
				count(1) sjfwzs,,
                        max(zm_buildings.town) town,
						max(zm_buildings.towndm) towndm,
            max(zm_buildings.xzq)xzq,
						 max(zm_buildings.xzqdm)xzqdm,
						  max(zm_buildings.village)village,
            zm_buildings.villagedm 
        FROM zm_buildings 
        GROUP BY zm_buildings.villagedm
        ORDER BY zm_buildings.villagedm
    ) AS t1
    LEFT JOIN (
         SELECT 
                count(1) ssltdbfjsl,village
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.village,jsrysstd, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE yhly='3' and sfyzc='是'  
        ) AS zm_pc_base where row_num =1  and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY village
    ) AS t2 ON t1.villagedm::text = t2.village::text
) 
 where t1.villagedm like @code
) jbxxgqview   ) a ORDER BY dbl desc,code 
 ";
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + param.code + "%"}).ToList();
                var res = new RespRet<object>(true, "Ok", data);
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                res.Msg = "系统异常";
                return res;
            }
        }



        /// <summary>
        /// 市核查达标率排名
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housessjhc/dblbar")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]

        public RespRet<object> SDblbar([FromBody] DateQuery param)
        {
            try
            {

                if (string.IsNullOrWhiteSpace(param.code))
                {
                    param.code = "110000";
                }
                param.code = param.code.Replace("000", "");

                string sql = @" ";
                if (param.code.Length == 3 )
                {
                    sql = @"  
select   zcount,(zcount-ydb) wdb,sjfwzs,ydb,name,code,CASE WHEN dbl > 100 THEN 100.00  when dbl is null then 0.00 ELSE dbl END dbl  from  (select ydb,zcount,sjfwzs,xzq name,xzqdm code,case when  round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(ydb*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (select max(jbxxgqview.xzq) xzq,jbxxgqview.xzqdm,sum(jbxxgqview.zcount) zcount,sum(ssltdbfjsl) ydb ,sum(sjfwzs) sjfwzs   from (SELECT 
    t1.xzq,
    t1.xzqdm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
   COALESCE((t1.sjfwzs)::numeric, (0)::numeric) AS sjfwzs,
		COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
             count(1) sjfwzs,
             count( case when fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='2' and fwzt is null ) and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') then fwbh end ) AS zcount,
            zm_buildings.xzq,
            zm_buildings.xzqdm 
        FROM zm_buildings   
        GROUP BY zm_buildings.xzqdm, zm_buildings.xzq
        ORDER BY zm_buildings.xzqdm
    ) AS t1
    LEFT JOIN (
       SELECT 
                count(1) ssltdbfjsl,xzqdm,xzq
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on, t1.xzq,jsrysstd, t1.xzqdm,t1.jzxyssltsl, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE  yhly='2' and sfyzc='是'  
        ) AS zm_pc_base where row_num =1   and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY xzqdm,xzq
    ) AS t2 ON t1.xzqdm::text = t2.xzqdm::text
) 
) jbxxgqview  GROUP BY xzqdm ) a ORDER BY dbl desc,xzqdm )a 	
	  ";
                }
                if (param.code.Length == 6)
                {
                    sql = @"  
  select  xzqdm,xzq,sjfwzs,zcount,(zcount-ydb) wdb,ydb,name,code,CASE WHEN dbl > 100 THEN 100.00  when dbl is null then 0.00 ELSE dbl END dbl  from  (select COALESCE(ssltdbfjsl,0) ydb,sjfwzs,zcount,xzqdm,xzq,town name,towndm code,case when  round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (SELECT 
    t1.xzq,
    t1.xzqdm,
		t1.town,
		t1.towndm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
   COALESCE((t1.sjfwzs)::numeric, (0)::numeric) AS sjfwzs,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
            count( case when fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='2' and fwzt is null ) and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') then fwbh end ) AS zcount,
            count(1)   sjfwzs,
                          max(zm_buildings.xzq)xzq,
						 max(zm_buildings.xzqdm)xzqdm,
						  max(zm_buildings.town)town,
            zm_buildings.towndm 
        FROM  zm_buildings
        GROUP BY zm_buildings.towndm
        ORDER BY zm_buildings.towndm
    ) AS t1
    LEFT JOIN (
        SELECT 
                count(1) ssltdbfjsl,town
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.town,jsrysstd, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE yhly='2'  and sfyzc='是'  
        ) AS zm_pc_base where row_num =1  and jsrysstd='是'  and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY town
    ) AS t2 ON t1.towndm::text = t2.town::text
) 
 where t1.towndm like @code
) jbxxgqview   ) a ORDER BY dbl desc,code ";
                }
                if (param.code.Length == 9)
                {
                    sql = @"select zcount,sjfwzs,(zcount-ydb) wdb,town,towndm,xzqdm,xzq,zcount,ydb,name,code,CASE WHEN dbl > 100 THEN 100.00  when dbl is null then 0.00 ELSE dbl END dbl  from  (select COALESCE(ssltdbfjsl,0) ydb,sjfwzs,zcount,xzqdm,xzq,village name,villagedm code,town,towndm,case when  round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (SELECT 
		t1.town,
		t1.towndm,
    t1.xzq,
    t1.xzqdm,
		t1.village,
		t1.villagedm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE((t1.sjfwzs)::numeric, (0)::numeric) AS sjfwzs,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
             count( case when fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='2' and fwzt is null  ) and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') then fwbh end ) AS zcount,
				        count(1) sjfwzs,
                        max(zm_buildings.town) town,
						max(zm_buildings.towndm) towndm,
            max(zm_buildings.xzq)xzq,
						 max(zm_buildings.xzqdm)xzqdm,
						  max(zm_buildings.village)village,
            zm_buildings.villagedm 
        FROM zm_buildings 
        GROUP BY zm_buildings.villagedm
        ORDER BY zm_buildings.villagedm
    ) AS t1
    LEFT JOIN (
         SELECT 
                count(1) ssltdbfjsl,village
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.village,jsrysstd, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE yhly='2'  and sfyzc='是'  
        ) AS zm_pc_base where row_num =1   and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY village
    ) AS t2 ON t1.villagedm::text = t2.village::text
) 
 where t1.villagedm like @code
) jbxxgqview   ) a ORDER BY dbl desc,code 
 ";
                }
                if (param.code.Length == 12)
                {
                    sql = @" select zcount,(zcount-ydb) wdb,sjfwzs,town,towndm,xzqdm,xzq,zcount,ydb,name,code,CASE WHEN dbl > 100 THEN 100.00  when dbl is null then 0.00 ELSE dbl END dbl  from  (select COALESCE(ssltdbfjsl,0) ydb,sjfwzs,zcount,xzqdm,xzq,village name,villagedm code,town,towndm,case when  round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) is null then 0.00 else round(COALESCE(ssltdbfjsl,0)*1.00*100/case when zcount =0 then null else zcount end ,2) end   dbl from  (SELECT 
		t1.town,
		t1.towndm,
    t1.xzq,
    t1.xzqdm,
		t1.village,
		t1.villagedm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
 COALESCE((t1.sjfwzs)::numeric, (0)::numeric) AS sjfwzs,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl
FROM (
    (
        SELECT 
             count( case when fwbh in (select distinct fwbh from zm_pc_base_sqjz where yhly='2' and fwzt is null ) and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') then fwbh end ) AS zcount,
				count(1) sjfwzs,,
                        max(zm_buildings.town) town,
						max(zm_buildings.towndm) towndm,
            max(zm_buildings.xzq)xzq,
						 max(zm_buildings.xzqdm)xzqdm,
						  max(zm_buildings.village)village,
            zm_buildings.villagedm 
        FROM zm_buildings 
        GROUP BY zm_buildings.villagedm
        ORDER BY zm_buildings.villagedm
    ) AS t1
    LEFT JOIN (
         SELECT 
                count(1) ssltdbfjsl,village
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.village,jsrysstd, t1.fwbh,fwzt 
            FROM  zm_pc_baselog_sqjz t1 
            JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE yhly='2'  and sfyzc='是'  
        ) AS zm_pc_base where row_num =1  and jsrysstd='是' and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') GROUP BY village
    ) AS t2 ON t1.villagedm::text = t2.village::text
) 
 where t1.villagedm like @code
) jbxxgqview   ) a ORDER BY dbl desc,code 
 ";
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + param.code + "%" }).ToList();
                var res = new RespRet<object>(true, "Ok", data);
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                res.Msg = "系统异常";
                return res;
            }
        }

        public partial class ZmPcYhLogCjDto
        {


            [Column("fwbh")]
            [StringLength(50)]
            public string? Fwbh { get; set; }


            [Column("hzxm")]
            [StringLength(255)]
            public string? Hzxm { get; set; }


            [Column("address")]
            public string? Address { get; set; }



            [Column("jzscxyaqcksl")]
            public int? Jzscxyaqcksl { get; set; }


            [Column("jzxyssltsl")]
            public int? jzxyssltsl { get; set; }


            [Column("cs")]
            public int? Cs { get; set; }


            [Column("jsrysstd")]
            public string? Jsrysstd { get; set; }


        }

        /// <summary>
        /// 房屋列表
        /// </summary>
        public class FwlbTj
        {



            public int OrderBy { get; set; } = 0;

            /// <summary>
            /// 是否降序排序
            /// </summary>
            public bool OrderDesc { get; set; } = false;


            public int? isdb { get; set; }//是否达标   1是0否

            public string code { get; set; }


            public string fwbh { get; set; }

            public string hzxm { get; set; }

            public string address { get; set; }
            /// <summary>
            /// 页码从0开始
            /// </summary>
            public int PageIndex { get; set; } = 0;

            /// <summary>
            /// 每页个数
            /// </summary>
            public int Limit { get; set; } = 50;


            public List<DateTime> date { get; set; }
        }


        /// <summary>
        /// 检查记录返回房屋
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("houseszc/searchcj-dbfw")]
        [Produces("application/json")]
        public RespRet<Page<ZmPcYhLogCjDto>> SearchAttrListZc([FromBody] FwlbTj pageRequest)
        {

            try
            {
                if (string.IsNullOrWhiteSpace(pageRequest.code))
                {
                    return new RespRet<Page<ZmPcYhLogCjDto>>(false, "村代码不能为空");
                }
                if (pageRequest.isdb != 1 && pageRequest.isdb != 2)
                {
                    return new RespRet<Page<ZmPcYhLogCjDto>>(false, "请选择正确类型");
                }
                var sbSql = new StringBuilder();
                var sbCountSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sbSqlWhere2 = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();

                sbSql.Append(@$" select t1.address,t1.hzxm,t1.fwbh,a.cs,a.jzscxyaqcksl,a.jzxyssltsl,a.jsrysstd from  zm_buildings t1 left join zm_pc_base a on a.fwbh=t1.fwbh  where t1.fwbh in( SELECT 
          
                DISTINCT CASE
                    WHEN ((zm_pc_base.cs <= 1) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN fwbh
                    WHEN ((zm_pc_base.cs >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2) AND (zm_pc_base.jzxyssltsl >= 2)) THEN fwbh
                END
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jzxyssltsl,t1.cs, t1.jzscxyaqcksl, t1.fwbh
             FROM zm_pc_yh t0  
     JOIN zm_pc_baselog t1 ON t0.fwbh = t1.fwbh 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh 
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE  t2.fwzt IS NULL and t1.village=@code 
        ) AS zm_pc_base  where row_num =1
       and village=@code)  ");
                sbCountSql.Append(@$" select count(1) count from  zm_buildings t1 left join zm_pc_base a on a.fwbh=t1.fwbh  where t1.fwbh  in( SELECT 
          
                DISTINCT CASE
                    WHEN ((zm_pc_base.cs <= 1) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN fwbh
                    WHEN ((zm_pc_base.cs >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2) AND (zm_pc_base.jzxyssltsl >= 2)) THEN fwbh
                END
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jzxyssltsl,t1.cs, t1.jzscxyaqcksl, t1.fwbh
             FROM zm_pc_yh t0  
     JOIN zm_pc_baselog t1 ON t0.fwbh = t1.fwbh 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh 
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE  t2.fwzt IS NULL and t1.village=@code 
        ) AS zm_pc_base  where row_num =1
       and village=@code)  ");
              
                if (!string.IsNullOrWhiteSpace(pageRequest.fwbh))
                {
                    sbCountSql.Append($@" and t1.fwbh like @fwbh ");
                    sbSql.Append($@" and t1.fwbh like @fwbh ");
                    sqlParams.Add(new NpgsqlParameter("fwbh", "%" + pageRequest.fwbh + "%"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.hzxm))
                {
                    sbCountSql.Append($@" and t1.hzxm like @hzxm ");
                    sbSql.Append($@" and t1.hzxm like @hzxm ");
                    sqlParams.Add(new NpgsqlParameter("hzxm", "%" + pageRequest.hzxm + "%"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.address))
                {
                    sbCountSql.Append($@" and t1.address like @address ");
                    sbSql.Append($@" and t1.address like @address ");
                    sqlParams.Add(new NpgsqlParameter("address", "%" + pageRequest.address + "%"));
                }

                var count = _dbContext.Database.GetDbConnection().Query<int>(sbCountSql.ToString(), new { @code = pageRequest.code, @fwbh ="%"+pageRequest.fwbh+"%", @hzxm = "%" + pageRequest.hzxm + "%", @address = "%" + pageRequest.address + "%" }).FirstOrDefault();
                sbSql.Append($@" order by fwbh ");
                sbSql.Append($@" limit @limit offset @offset");
                sqlParams.Add(new NpgsqlParameter("code", pageRequest.code));
                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));
                if (pageRequest.isdb != 1)
                {
                    sbSql = new StringBuilder();
                    sbCountSql = new StringBuilder();
                    sbSqlWhere = new StringBuilder();
                    sbSqlWhere2 = new StringBuilder();
                    sqlParams = new List<NpgsqlParameter>();

                    sbSql.Append(@$" SELECT t1.address,t1.hzxm,t1.fwbh,a.cs,a.jzscxyaqcksl,a.jzxyssltsl,a.jsrysstd
     FROM zm_buildings t1  left join zm_pc_base a on a.fwbh=t1.fwbh  
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh::text = t2.fwbh::text WHERE fwzt IS NULL and t1.fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') and t1.fwbh not in( SELECT 
          
                DISTINCT CASE
                    WHEN ((zm_pc_base.cs <= 1) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN fwbh
                    WHEN ((zm_pc_base.cs >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2) AND (zm_pc_base.jzxyssltsl >= 2)) THEN fwbh
                    else '0'
                END
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jzxyssltsl,t1.cs, t1.jzscxyaqcksl, t1.fwbh
             FROM zm_pc_yh t0  
     JOIN zm_pc_baselog t1 ON t0.fwbh = t1.fwbh 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh 
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE  t2.fwzt IS NULL and t1.village=@code 
        ) AS zm_pc_base  where row_num =1
       and village=@code)  and t1.villagedm =@code ");
                    sbCountSql.Append(@$" SELECT count(1) count
     FROM zm_buildings t1   left join zm_pc_base a on a.fwbh=t1.fwbh  
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh::text = t2.fwbh::text WHERE fwzt IS NULL and t1.fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') and t1.fwbh not in( SELECT 
          
                DISTINCT CASE
                    WHEN ((zm_pc_base.cs <= 1) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN fwbh
                    WHEN ((zm_pc_base.cs >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2) AND (zm_pc_base.jzxyssltsl >= 2)) THEN fwbh
                    else '0'
                END
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jzxyssltsl,t1.cs, t1.jzscxyaqcksl, t1.fwbh
             FROM zm_pc_yh t0  
     JOIN zm_pc_baselog t1 ON t0.fwbh = t1.fwbh 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh 
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE  t2.fwzt IS NULL and t1.village=@code 
        ) AS zm_pc_base  where row_num =1
       and village=@code)  and t1.villagedm =@code  ");
                    if (!string.IsNullOrWhiteSpace(pageRequest.fwbh))
                    {
                        sbCountSql.Append($@" and t1.fwbh like @fwbh ");
                        sbSql.Append($@" and t1.fwbh like @fwbh ");
                        sqlParams.Add(new NpgsqlParameter("fwbh", "%" + pageRequest.fwbh + "%"));
                    }
                    if (!string.IsNullOrWhiteSpace(pageRequest.hzxm))
                    {
                        sbCountSql.Append($@" and t1.hzxm like @hzxm ");
                        sbSql.Append($@" and t1.hzxm like @hzxm ");
                        sqlParams.Add(new NpgsqlParameter("hzxm", "%" + pageRequest.hzxm + "%"));
                    }
                    if (!string.IsNullOrWhiteSpace(pageRequest.address))
                    {
                        sbCountSql.Append($@" and t1.address like @address ");
                        sbSql.Append($@" and t1.address like @address ");
                        sqlParams.Add(new NpgsqlParameter("address", "%" + pageRequest.address + "%"));
                    }
                    count = _dbContext.Database.GetDbConnection().Query<int>(sbCountSql.ToString(), new { @code = pageRequest.code, @fwbh = "%" + pageRequest.fwbh + "%", @hzxm = "%" + pageRequest.hzxm + "%", @address = "%" + pageRequest.address + "%" }).FirstOrDefault();
                    sbSql.Append($@" order by fwbh ");
                    sbSql.Append($@" limit @limit offset @offset");
                    sqlParams.Add(new NpgsqlParameter("code", pageRequest.code));
                    sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                    sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));
                }

               

                var datas = _dbContext.ZmPcYhLogCjDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).ToList();
                var pageData = new Page<ZmPcYhLogCjDto>(datas
                    , pageRequest.PageIndex + 1, pageRequest.Limit, count);
                var r = new RespRet<Page<ZmPcYhLogCjDto>>(true, "Ok", pageData);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<Page<ZmPcYhLogCjDto>>(false, ex.ToString());
                res.Msg = "系统异常";
                return res;
            }
        }




        /// <summary>
        /// 信息统计 -村级检查达标、未达标房屋
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescjjc/searchcj-dbfw")]
        [Produces("application/json")]
        public RespRet<Page<ZmPcYhLogCjDto>> SearchAttrList2([FromBody] FwlbTj pageRequest)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(pageRequest.code))
                {
                    return new RespRet<Page<ZmPcYhLogCjDto>>(false, "村代码不能为空");
                }
                if (pageRequest.isdb != 1 && pageRequest.isdb != 2)
                {
                    return new RespRet<Page<ZmPcYhLogCjDto>>(false, "请选择正确类型");
                }
                var sbSql = new StringBuilder();
                var sbCountSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sbSqlWhere2 = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();

                sbSql.Append(@$" select t1.address,t1.hzxm,t1.fwbh,a.cs,a.jzscxyaqcksl,a.jzxyssltsl,a.jsrysstd from  zm_buildings t1 left join zm_pc_base_cj a on a.fwbh=t1.fwbh  where t1.fwbh in ( SELECT 
          
                DISTINCT CASE when jsrysstd='是' THEN fwbh
                END
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jsrysstd, t1.fwbh
             FROM zm_pc_yh_cj t0  
     JOIN zm_pc_baselog_cj t1 ON t0.fwbh = t1.fwbh 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh 
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE   t2.fwzt IS NULL  and t1.village=@code 
        ) AS zm_pc_base  where row_num =1  and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')
       and village=@code) ");
                sbCountSql.Append(@$" select count(1) count from  zm_buildings t1 left join zm_pc_base_cj a on a.fwbh=t1.fwbh  where t1.fwbh in ( SELECT 
          
               DISTINCT CASE when jsrysstd='是' THEN fwbh 
                END
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jsrysstd, t1.fwbh
             FROM zm_pc_yh_cj t0  
     JOIN zm_pc_baselog_cj t1 ON t0.fwbh = t1.fwbh 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh 
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE  t2.fwzt IS NULL and t1.village=@code 
        ) AS zm_pc_base  where row_num =1  and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')
       and village=@code)  ");
                if (!string.IsNullOrWhiteSpace(pageRequest.fwbh))
                {
                    sbCountSql.Append($@" and t1.fwbh like @fwbh ");
                    sbSql.Append($@" and t1.fwbh like @fwbh ");
                    sqlParams.Add(new NpgsqlParameter("fwbh", "%" + pageRequest.fwbh + "%"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.hzxm))
                {
                    sbCountSql.Append($@" and t1.hzxm like @hzxm ");
                    sbSql.Append($@" and t1.hzxm like @hzxm ");
                    sqlParams.Add(new NpgsqlParameter("hzxm", "%" + pageRequest.hzxm + "%"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.address))
                {
                    sbCountSql.Append($@" and t1.address like @address ");
                    sbSql.Append($@" and t1.address like @address ");
                    sqlParams.Add(new NpgsqlParameter("address", "%" + pageRequest.address + "%"));
                }
                var count = _dbContext.Database.GetDbConnection().Query<int>(sbCountSql.ToString(), new { @code = pageRequest.code, @fwbh = "%" + pageRequest.fwbh + "%", @hzxm = "%" + pageRequest.hzxm + "%", @address = "%" + pageRequest.address + "%" }).FirstOrDefault();
                sbSql.Append($@" order by fwbh ");
                sbSql.Append($@" limit @limit offset @offset");
                sqlParams.Add(new NpgsqlParameter("code", pageRequest.code));
                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));
                if (pageRequest.isdb != 1)
                {
                    sbSql = new StringBuilder();
                    sbCountSql = new StringBuilder();
                    sbSqlWhere = new StringBuilder();
                    sbSqlWhere2 = new StringBuilder();
                    sqlParams = new List<NpgsqlParameter>();

                    sbSql.Append(@$" SELECT t1.address,t1.hzxm,t1.fwbh,a.cs,a.jzscxyaqcksl,a.jzxyssltsl,a.jsrysstd
     FROM zm_buildings t1
     LEFT JOIN zm_pc_base_cj a ON t1.fwbh::text = a.fwbh::text WHERE fwzt IS NULL  and t1.fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') and t1.fwbh not in( SELECT 
          
                DISTINCT CASE when jsrysstd='是' THEN fwbh else '0' end
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jsrysstd, t1.fwbh
             FROM zm_pc_yh_cj t0  
     JOIN zm_pc_baselog_cj t1 ON t0.fwbh = t1.fwbh 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh 
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE  t2.fwzt IS NULL and t1.village=@code  
        ) AS zm_pc_base  where row_num =1 
       and village=@code)  and t1.villagedm =@code ");
                    sbCountSql.Append(@$" SELECT count(1) count
     FROM zm_buildings t1 
     LEFT JOIN zm_pc_base_cj a ON t1.fwbh::text = a.fwbh::text WHERE fwzt IS NULL  and t1.fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') and t1.fwbh not in( SELECT 
               DISTINCT CASE when jsrysstd='是' THEN fwbh else '0' end
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jsrysstd, t1.fwbh
             FROM zm_pc_yh_cj t0  
     JOIN zm_pc_baselog_cj t1 ON t0.fwbh = t1.fwbh 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh 
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE  t2.fwzt IS NULL and t1.village=@code  
        ) AS zm_pc_base  where row_num =1  and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')
       and village=@code)  and t1.villagedm =@code  ");
                    if (!string.IsNullOrWhiteSpace(pageRequest.fwbh))
                    {
                        sbCountSql.Append($@" and t1.fwbh like @fwbh ");
                        sbSql.Append($@" and t1.fwbh like @fwbh ");
                        sqlParams.Add(new NpgsqlParameter("fwbh", "%" + pageRequest.fwbh + "%"));
                    }
                    if (!string.IsNullOrWhiteSpace(pageRequest.hzxm))
                    {
                        sbCountSql.Append($@" and t1.hzxm like @hzxm ");
                        sbSql.Append($@" and t1.hzxm like @hzxm ");
                        sqlParams.Add(new NpgsqlParameter("hzxm", "%" + pageRequest.hzxm + "%"));
                    }
                    if (!string.IsNullOrWhiteSpace(pageRequest.address))
                    {
                        sbCountSql.Append($@" and t1.address like @address ");
                        sbSql.Append($@" and t1.address like @address ");
                        sqlParams.Add(new NpgsqlParameter("address", "%" + pageRequest.address + "%"));
                    }
                    count = _dbContext.Database.GetDbConnection().Query<int>(sbCountSql.ToString(), new { @code = pageRequest.code, @fwbh = "%" + pageRequest.fwbh + "%", @hzxm = "%" + pageRequest.hzxm + "%", @address = "%" + pageRequest.address + "%" }).FirstOrDefault();
                    sbSql.Append($@" order by fwbh ");
                    sbSql.Append($@" limit @limit offset @offset");
                    sqlParams.Add(new NpgsqlParameter("code", pageRequest.code));
                    sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                    sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));
                }

               
                var datas = _dbContext.ZmPcYhLogCjDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).ToList();
                var pageData = new Page<ZmPcYhLogCjDto>(datas
                    , pageRequest.PageIndex + 1, pageRequest.Limit, count);
                var r = new RespRet<Page<ZmPcYhLogCjDto>>(true, "Ok", pageData);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<Page<ZmPcYhLogCjDto>>(false, ex.ToString());
                res.Msg = "系统异常";
                return res;
            }
        }





        /// <summary>
        /// 信息统计 --街镇核查达标、未达标房屋
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housesjzhc/searchcj-dbfw")]
        [Produces("application/json")]
        public RespRet<Page<ZmPcYhLogCjDto>> SearchAttrList3([FromBody] FwlbTj pageRequest)
        {

            try
            {
                if (string.IsNullOrWhiteSpace(pageRequest.code))
                {
                    return new RespRet<Page<ZmPcYhLogCjDto>>(false, "村代码不能为空");
                }
                if (pageRequest.isdb != 1 && pageRequest.isdb != 2)
                {
                    return new RespRet<Page<ZmPcYhLogCjDto>>(false, "请选择正确类型");
                }

                var sbSql = new StringBuilder();
                var sbCountSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sbSqlWhere2 = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();

                sbSql.Append(@$" select t1.address,t1.hzxm,t1.fwbh,a.cs,a.jzscxyaqcksl,a.jzxyssltsl,a.jsrysstd from  zm_buildings t1 left join (select fwbh,jzscxyaqcksl,jsrysstd,jzxyssltsl,cs From zm_pc_base_sqjz where fwzt is  null and yhly='4') a on a.fwbh=t1.fwbh  where t1.fwbh in( SELECT 
          
             DISTINCT CASE when jsrysstd='是' THEN fwbh end
        FROM (
                        SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jsrysstd, t1.fwbh
             FROM zm_pc_yh_sqjz t0  
     JOIN zm_pc_baselog_sqjz t1 ON t0.fwbh = t1.fwbh 
 LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE  t0.jcly='4' and t1.yhly='4' and t2.fwzt is  null
        ) AS zm_pc_base  where row_num =1  and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')
       and village=@code) ");
                sbCountSql.Append(@$" select count(1) count from  zm_buildings t1 left join (select fwbh From zm_pc_base_sqjz where fwzt is  null and yhly='4') a on a.fwbh=t1.fwbh where t1.fwbh in( SELECT 
          
              DISTINCT CASE when jsrysstd='是' THEN fwbh end
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jsrysstd, t1.fwbh
             FROM zm_pc_yh_sqjz t0  
     JOIN zm_pc_baselog_sqjz t1 ON t0.fwbh = t1.fwbh 
LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE  t0.jcly='4' and t1.yhly='4' and t2.fwzt is null
        ) AS zm_pc_base  where row_num =1  and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')
       and village=@code)  ");
                if (!string.IsNullOrWhiteSpace(pageRequest.fwbh))
                {
                    sbCountSql.Append($@" and t1.fwbh like @fwbh ");
                    sbSql.Append($@" and t1.fwbh like @fwbh ");
                    sqlParams.Add(new NpgsqlParameter("fwbh", "%" + pageRequest.fwbh + "%"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.hzxm))
                {
                    sbCountSql.Append($@" and t1.hzxm like @hzxm ");
                    sbSql.Append($@" and t1.hzxm like @hzxm ");
                    sqlParams.Add(new NpgsqlParameter("hzxm", "%" + pageRequest.hzxm + "%"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.address))
                {
                    sbCountSql.Append($@" and t1.address like @address ");
                    sbSql.Append($@" and t1.address like @address ");
                    sqlParams.Add(new NpgsqlParameter("address", "%" + pageRequest.address + "%"));
                }
                var count = _dbContext.Database.GetDbConnection().Query<int>(sbCountSql.ToString(), new { @code = pageRequest.code, @fwbh = "%" + pageRequest.fwbh + "%", @hzxm = "%" + pageRequest.hzxm + "%", @address = "%" + pageRequest.address + "%" }).FirstOrDefault();
                sbSql.Append($@" order by fwbh ");
                sbSql.Append($@" limit @limit offset @offset");
                sqlParams.Add(new NpgsqlParameter("code", pageRequest.code));
                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));
                if (pageRequest.isdb != 1)
                {
                    sbSql = new StringBuilder();
                    sbCountSql = new StringBuilder();
                    sbSqlWhere = new StringBuilder();
                    sbSqlWhere2 = new StringBuilder();
                    sqlParams = new List<NpgsqlParameter>();
                    sbSql.Append(@$" SELECT t1.address,t1.hzxm,t1.fwbh,a.cs,a.jzscxyaqcksl,a.jzxyssltsl,a.jsrysstd
     FROM zm_buildings t1 left join (select fwbh,jzscxyaqcksl,jsrysstd,jzxyssltsl,cs From zm_pc_base_sqjz where fwzt is  null and yhly='4') a on a.fwbh=t1.fwbh  where t1.fwbh not in( SELECT 
          
                DISTINCT CASE when jsrysstd='是' THEN fwbh  else '0' end
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jsrysstd, t1.fwbh
             FROM zm_pc_yh_sqjz t0  
     JOIN zm_pc_baselog_sqjz t1 ON t0.fwbh = t1.fwbh 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE  t0.jcly='4' and t1.yhly='4' and t2.fwzt is null
        ) AS zm_pc_base  where row_num =1
       and village=@code) and t1.fwbh in (select fwbh from  zm_pc_base_sqjz where village=@code and yhly='4'   and fwbh not in (select fwbh from zm_pc_base_cj where fwzt is not null)) and t1.villagedm =@code ");
                    sbCountSql.Append(@$" SELECT count(1) count
     FROM zm_buildings t1 left join (select fwbh From zm_pc_base_sqjz where fwzt is  null and yhly='4') a on a.fwbh=t1.fwbh  where t1.fwbh not in( SELECT 
          
               DISTINCT CASE when jsrysstd='是' THEN fwbh  else '0' end
        FROM (
           SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jsrysstd, t1.fwbh
             FROM zm_pc_yh_sqjz t0  
     JOIN zm_pc_baselog_sqjz t1 ON t0.fwbh = t1.fwbh 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE  t0.jcly='4' and t1.yhly='4' and t2.fwzt is null
        ) AS zm_pc_base  where row_num =1
       and village=@code) and t1.fwbh in (select fwbh from  zm_pc_base_sqjz where village=@code and yhly='4'  and fwbh not in (select fwbh from zm_pc_base_cj where fwzt is not null))  and t1.villagedm =@code  ");
                    if (!string.IsNullOrWhiteSpace(pageRequest.fwbh))
                    {
                        sbCountSql.Append($@" and t1.fwbh like @fwbh ");
                        sbSql.Append($@" and t1.fwbh like @fwbh ");
                        sqlParams.Add(new NpgsqlParameter("fwbh", "%" + pageRequest.fwbh + "%"));
                    }
                    if (!string.IsNullOrWhiteSpace(pageRequest.hzxm))
                    {
                        sbCountSql.Append($@" and t1.hzxm like @hzxm ");
                        sbSql.Append($@" and t1.hzxm like @hzxm ");
                        sqlParams.Add(new NpgsqlParameter("hzxm", "%" + pageRequest.hzxm + "%"));
                    }
                    if (!string.IsNullOrWhiteSpace(pageRequest.address))
                    {
                        sbCountSql.Append($@" and t1.address like @address ");
                        sbSql.Append($@" and t1.address like @address ");
                        sqlParams.Add(new NpgsqlParameter("address", "%" + pageRequest.address + "%"));
                    }
                    count = _dbContext.Database.GetDbConnection().Query<int>(sbCountSql.ToString(), new { @code = pageRequest.code, @fwbh = "%" + pageRequest.fwbh + "%", @hzxm = "%" + pageRequest.hzxm + "%", @address = "%" + pageRequest.address + "%" }).FirstOrDefault();
                    sbSql.Append($@" order by fwbh ");
                    sbSql.Append($@" limit @limit offset @offset");
                    sqlParams.Add(new NpgsqlParameter("code", pageRequest.code));
                    sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                    sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));
                }

              
                var datas = _dbContext.ZmPcYhLogCjDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).ToList();
                var pageData = new Page<ZmPcYhLogCjDto>(datas
                    , pageRequest.PageIndex + 1, pageRequest.Limit, count);
                var r = new RespRet<Page<ZmPcYhLogCjDto>>(true, "Ok", pageData);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<Page<ZmPcYhLogCjDto>>(false, ex.ToString());
                res.Msg = "系统异常";
                return res;


            }
        }





        /// <summary>
        /// 信息统计 -区级核查达标、未达标房屋
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housesqjhc/searchcj-dbfw")]
        [Produces("application/json")]
        public RespRet<Page<ZmPcYhLogCjDto>> SearchAttrList4([FromBody] FwlbTj pageRequest)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(pageRequest.code))
                {
                    return new RespRet<Page<ZmPcYhLogCjDto>>(false, "村代码不能为空");
                }
                if (pageRequest.isdb != 1 && pageRequest.isdb != 2)
                {
                    return new RespRet<Page<ZmPcYhLogCjDto>>(false, "请选择正确类型");
                }

                var sbSql = new StringBuilder();
                var sbCountSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sbSqlWhere2 = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();

                sbSql.Append(@$" select t1.address,t1.hzxm,t1.fwbh,a.cs,a.jzscxyaqcksl,a.jzxyssltsl,a.jsrysstd from  zm_buildings t1 left join (select fwbh,jzscxyaqcksl,jsrysstd,jzxyssltsl,cs From zm_pc_base_sqjz where   yhly='3') a on a.fwbh=t1.fwbh  where t1.fwbh in( SELECT 
          
             DISTINCT CASE when jsrysstd='是' THEN fwbh  end
        FROM (
                        SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jsrysstd, t1.fwbh
             FROM zm_pc_yh_sqjz t0  
     JOIN zm_pc_baselog_sqjz t1 ON t0.fwbh = t1.fwbh 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE  t0.jcly='3' and t1.yhly='3' and t2.fwzt is  null 
        ) AS zm_pc_base  where row_num =1  and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')
       and village=@code)  ");
                sbCountSql.Append(@$" select count(1) count from  zm_buildings t1 left join (select fwbh From zm_pc_base_sqjz where   yhly='3') a on a.fwbh=t1.fwbh where t1.fwbh  in( SELECT 
          DISTINCT CASE when jsrysstd='是' THEN fwbh end
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jsrysstd, t1.fwbh
             FROM zm_pc_yh_sqjz t0  
     JOIN zm_pc_baselog_sqjz t1 ON t0.fwbh = t1.fwbh 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE  t0.jcly='3' and t1.yhly='3' and t2.fwzt is null
        ) AS zm_pc_base  where row_num =1  and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')
       and village=@code)  ");
                if (!string.IsNullOrWhiteSpace(pageRequest.fwbh))
                {
                    sbCountSql.Append($@" and t1.fwbh like @fwbh ");
                    sbSql.Append($@" and t1.fwbh like @fwbh ");
                    sqlParams.Add(new NpgsqlParameter("fwbh", "%" + pageRequest.fwbh + "%"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.hzxm))
                {
                    sbCountSql.Append($@" and t1.hzxm like @hzxm ");
                    sbSql.Append($@" and t1.hzxm like @hzxm ");
                    sqlParams.Add(new NpgsqlParameter("hzxm", "%" + pageRequest.hzxm + "%"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.address))
                {
                    sbCountSql.Append($@" and t1.address like @address ");
                    sbSql.Append($@" and t1.address like @address ");
                    sqlParams.Add(new NpgsqlParameter("address", "%" + pageRequest.address + "%"));
                }
                var count = _dbContext.Database.GetDbConnection().Query<int>(sbCountSql.ToString(), new { @code = pageRequest.code, @fwbh = "%" + pageRequest.fwbh + "%", @hzxm = "%" + pageRequest.hzxm + "%", @address = "%" + pageRequest.address + "%" }).FirstOrDefault();
                sbSql.Append($@" order by fwbh ");
                sbSql.Append($@" limit @limit offset @offset");
                sqlParams.Add(new NpgsqlParameter("code", pageRequest.code));
                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));
                if (pageRequest.isdb != 1)
                {
                    sbSql = new StringBuilder();
                    sbCountSql = new StringBuilder();
                    sbSqlWhere = new StringBuilder();
                    sbSqlWhere2 = new StringBuilder();
                    sqlParams = new List<NpgsqlParameter>();

                    sbSql.Append(@$" SELECT t1.address,t1.hzxm,t1.fwbh,a.cs,a.jzscxyaqcksl,a.jzxyssltsl,a.jsrysstd
     FROM zm_buildings t1 left join (select fwbh,jzscxyaqcksl,jsrysstd,jzxyssltsl,cs From zm_pc_base_sqjz where   yhly='3') a on a.fwbh=t1.fwbh where t1.fwbh not in( SELECT 
          
              DISTINCT CASE when jsrysstd='是' THEN fwbh  else '0' end
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jsrysstd, t1.fwbh
             FROM zm_pc_yh_sqjz t0  
     JOIN zm_pc_baselog_sqjz t1 ON t0.fwbh = t1.fwbh 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE  t0.jcly='3' and t1.yhly='3' and t2.fwzt is null
        ) AS zm_pc_base  where row_num =1
       and village=@code) and t1.fwbh in (select fwbh from  zm_pc_base_sqjz where village=@code and yhly='3'  and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')  and fwbh not in (select fwbh from zm_pc_base_cj where fwzt is not null)  ) and t1.villagedm =@code ");
                    sbCountSql.Append(@$" SELECT count(1) count
     FROM zm_buildings t1 left join (select fwbh From zm_pc_base_sqjz where   yhly='3') a on a.fwbh=t1.fwbh where t1.fwbh not in( SELECT 
          
              DISTINCT CASE when jsrysstd='是' THEN fwbh  else '0' end
        FROM (
           SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jsrysstd, t1.fwbh
             FROM zm_pc_yh_sqjz t0  
     JOIN zm_pc_baselog_sqjz t1 ON t0.fwbh = t1.fwbh 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE  t0.jcly='3' and t1.yhly='3' and t2.fwzt is null
        ) AS zm_pc_base  where row_num =1
       and village=@code) and t1.fwbh in (select fwbh from  zm_pc_base_sqjz where village=@code and yhly='3'  and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') and fwbh not in (select fwbh from zm_pc_base_cj where fwzt is not null) )  and t1.villagedm =@code  ");
                    if (!string.IsNullOrWhiteSpace(pageRequest.fwbh))
                    {
                        sbCountSql.Append($@" and t1.fwbh like @fwbh ");
                        sbSql.Append($@" and t1.fwbh like @fwbh ");
                        sqlParams.Add(new NpgsqlParameter("fwbh", "%" + pageRequest.fwbh + "%"));
                    }
                    if (!string.IsNullOrWhiteSpace(pageRequest.hzxm))
                    {
                        sbCountSql.Append($@" and t1.hzxm like @hzxm ");
                        sbSql.Append($@" and t1.hzxm like @hzxm ");
                        sqlParams.Add(new NpgsqlParameter("hzxm", "%" + pageRequest.hzxm + "%"));
                    }
                    if (!string.IsNullOrWhiteSpace(pageRequest.address))
                    {
                        sbCountSql.Append($@" and t1.address like @address ");
                        sbSql.Append($@" and t1.address like @address ");
                        sqlParams.Add(new NpgsqlParameter("address", "%" + pageRequest.address + "%"));
                    }
                    count = _dbContext.Database.GetDbConnection().Query<int>(sbCountSql.ToString(), new { @code = pageRequest.code, @fwbh = "%" + pageRequest.fwbh + "%", @hzxm = "%" + pageRequest.hzxm + "%", @address = "%" + pageRequest.address + "%" }).FirstOrDefault();
                    sbSql.Append($@" order by fwbh ");
                    sbSql.Append($@" limit @limit offset @offset");
                    sqlParams.Add(new NpgsqlParameter("code", pageRequest.code));
                    sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                    sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));
                }

                var datas = _dbContext.ZmPcYhLogCjDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).ToList();
                var pageData = new Page<ZmPcYhLogCjDto>(datas
                    , pageRequest.PageIndex + 1, pageRequest.Limit, count);
                var r = new RespRet<Page<ZmPcYhLogCjDto>>(true, "Ok", pageData);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<Page<ZmPcYhLogCjDto>>(false, ex.ToString());
                res.Msg = "系统异常";
                return res;


            }
        }


        /// <summary>
        /// 信息统计 -市级核查达标、未达标房屋
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housessjhc/searchcj-dbfw")]
        [Produces("application/json")]
        public RespRet<Page<ZmPcYhLogCjDto>> SearchAttrList5([FromBody] FwlbTj pageRequest)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(pageRequest.code))
                {
                    return new RespRet<Page<ZmPcYhLogCjDto>>(false, "村代码不能为空");
                }
                if (pageRequest.isdb != 1 && pageRequest.isdb != 2)
                {
                    return new RespRet<Page<ZmPcYhLogCjDto>>(false, "请选择正确类型");
                }

              
                var sbSql = new StringBuilder();
                var sbCountSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sbSqlWhere2 = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();

                sbSql.Append(@$" select t1.address,t1.hzxm,t1.fwbh,a.cs,a.jzscxyaqcksl,a.jzxyssltsl,a.jsrysstd from  zm_buildings t1 left join (select fwbh,jzscxyaqcksl,jsrysstd,jzxyssltsl,cs From zm_pc_base_sqjz where   yhly='3') a on a.fwbh=t1.fwbh where t1.fwbh in( SELECT 
          
                DISTINCT CASE when jsrysstd='是' THEN fwbh
                END
        FROM (
                        SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jsrysstd, t1.fwbh
             FROM zm_pc_yh_sqjz t0  
     JOIN zm_pc_baselog_sqjz t1 ON t0.fwbh = t1.fwbh 
    LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE   t0.jcly='2' and t1.yhly='2' and t2.fwzt is null
        ) AS zm_pc_base  where row_num =1  and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')
       and village=@code) ");
                sbCountSql.Append(@$" select count(1) count from  zm_buildings t1 left join (select fwbh From zm_pc_base_sqjz where   yhly='3') a on a.fwbh=t1.fwbh where t1.fwbh in( SELECT 
          
                  DISTINCT CASE when jsrysstd='是' THEN fwbh
                END
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jsrysstd, t1.fwbh
             FROM zm_pc_yh_sqjz t0  
     JOIN zm_pc_baselog_sqjz t1 ON t0.fwbh = t1.fwbh 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE  t0.jcly='2' and t1.yhly='2' and t2.fwzt is null
        ) AS zm_pc_base  where row_num =1  and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')
       and village=@code)  ");
                if (!string.IsNullOrWhiteSpace(pageRequest.fwbh))
                {
                    sbCountSql.Append($@" and t1.fwbh like @fwbh ");
                    sbSql.Append($@" and t1.fwbh like @fwbh ");
                    sqlParams.Add(new NpgsqlParameter("fwbh", "%" + pageRequest.fwbh + "%"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.hzxm))
                {
                    sbCountSql.Append($@" and t1.hzxm like @hzxm ");
                    sbSql.Append($@" and t1.hzxm like @hzxm ");
                    sqlParams.Add(new NpgsqlParameter("hzxm", "%" + pageRequest.hzxm + "%"));
                }
                if (!string.IsNullOrWhiteSpace(pageRequest.address))
                {
                    sbCountSql.Append($@" and t1.address like @address ");
                    sbSql.Append($@" and t1.address like @address ");
                    sqlParams.Add(new NpgsqlParameter("address", "%" + pageRequest.address + "%"));
                }
                var count = _dbContext.Database.GetDbConnection().Query<int>(sbCountSql.ToString(), new { @code = pageRequest.code, @fwbh = "%" + pageRequest.fwbh + "%", @hzxm = "%" + pageRequest.hzxm + "%", @address = "%" + pageRequest.address + "%" }).FirstOrDefault();
                sbSql.Append($@" order by fwbh ");
                sbSql.Append($@" limit @limit offset @offset");
                sqlParams.Add(new NpgsqlParameter("code", pageRequest.code));
                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));
                if (pageRequest.isdb != 1)
                {
                    sbSql = new StringBuilder();
                    sbCountSql = new StringBuilder();
                    sbSqlWhere = new StringBuilder();
                    sbSqlWhere2 = new StringBuilder();
                    sqlParams = new List<NpgsqlParameter>();

                    sbSql.Append(@$" SELECT t1.address,t1.hzxm,t1.fwbh,a.cs,a.jzscxyaqcksl,a.jzxyssltsl,a.jsrysstd
     FROM zm_buildings t1 left join (select fwbh,jzscxyaqcksl,jsrysstd,jzxyssltsl,cs From zm_pc_base_sqjz where   yhly='3') a on a.fwbh=t1.fwbh where t1.fwbh not in( SELECT 
          
                 DISTINCT CASE when jsrysstd='是' THEN  fwbh  else '0'
                END
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,t1.jsrysstd, t1.fwbh
             FROM zm_pc_yh_sqjz t0  
     JOIN zm_pc_baselog_sqjz t1 ON t0.fwbh = t1.fwbh 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE  t0.jcly='2' and t1.yhly='2' and t2.fwzt is null
        ) AS zm_pc_base  where row_num =1
       and village=@code) and t1.fwbh in (select fwbh from  zm_pc_base_sqjz where village=@code and yhly='2'  and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') and fwbh not in (select fwbh from zm_pc_base_cj where fwzt is not null)) and t1.villagedm =@code ");
                    sbCountSql.Append(@$" SELECT count(1) count
     FROM zm_buildings t1 left join (select fwbh From zm_pc_base_sqjz where   yhly='3') a on a.fwbh=t1.fwbh where t1.fwbh not in( SELECT 
            DISTINCT CASE when jsrysstd='是' THEN fwbh  else '0'
                END
        FROM (
           SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.jsrysstd, t1.fwbh
             FROM zm_pc_yh_sqjz t0  
     JOIN zm_pc_baselog_sqjz t1 ON t0.fwbh = t1.fwbh 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh
     JOIN zm_buildings t5 ON t0.fwbh = t5.fwbh 
            WHERE  t0.jcly='2' and t1.yhly='2' and t2.fwzt is null
        ) AS zm_pc_base  where row_num =1
       and village=@code) and t1.fwbh in (select fwbh from  zm_pc_base_sqjz where village=@code and yhly='2' and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4')  and fwbh not in (select fwbh from zm_pc_base_cj where fwzt is not null)  )  and t1.villagedm =@code  ");
                    if (!string.IsNullOrWhiteSpace(pageRequest.fwbh))
                    {
                        sbCountSql.Append($@" and t1.fwbh like @fwbh ");
                        sbSql.Append($@" and t1.fwbh like @fwbh ");
                        sqlParams.Add(new NpgsqlParameter("fwbh", "%" + pageRequest.fwbh + "%"));
                    }
                    if (!string.IsNullOrWhiteSpace(pageRequest.hzxm))
                    {
                        sbCountSql.Append($@" and t1.hzxm like @hzxm ");
                        sbSql.Append($@" and t1.hzxm like @hzxm ");
                        sqlParams.Add(new NpgsqlParameter("hzxm", "%" + pageRequest.hzxm + "%"));
                    }
                    if (!string.IsNullOrWhiteSpace(pageRequest.address))
                    {
                        sbCountSql.Append($@" and t1.address like @address ");
                        sbSql.Append($@" and t1.address like @address ");
                        sqlParams.Add(new NpgsqlParameter("address", "%" + pageRequest.address + "%"));
                    }
                    count = _dbContext.Database.GetDbConnection().Query<int>(sbCountSql.ToString(), new { @code = pageRequest.code, @fwbh = "%" + pageRequest.fwbh + "%", @hzxm = "%" + pageRequest.hzxm + "%", @address = "%" + pageRequest.address + "%" }).FirstOrDefault();
                    sbSql.Append($@" order by fwbh ");
                    sbSql.Append($@" limit @limit offset @offset");
                    sqlParams.Add(new NpgsqlParameter("code", pageRequest.code));
                    sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                    sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));
                }

                
                var datas = _dbContext.ZmPcYhLogCjDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).ToList();
                var pageData = new Page<ZmPcYhLogCjDto>(datas
                    , pageRequest.PageIndex + 1, pageRequest.Limit, count);
                var r = new RespRet<Page<ZmPcYhLogCjDto>>(true, "Ok", pageData);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<Page<ZmPcYhLogCjDto>>(false, ex.ToString());
                res.Msg = "系统异常";
                return res;


            }
        }



        /// <summary>
        /// 统计核验变化情况（市级）
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpGet("housessjhc/otherbaseYh")]
        [Produces("application/json")]
        public RespRet<Page<object>>  otherbaseYh([FromQuery] string xzqdm, [FromQuery] string towndm, [FromQuery] string villagedm, [FromQuery] int pageindex, [FromQuery] int pagesize, [FromQuery] string startTime, [FromQuery] string endTime, [FromQuery] string txtKeyWord, [FromQuery] string cked)
        {
            if (pageindex < 0)
            {
                return new RespRet<Page<object>>(false, "PageIndex must >= 0");
            }

            var userNumber = this.CurrentUser();
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<Page<object>>(false, "user not logined");
            }
            var user = _userRepository.FirstOrDefault(u => u.Number == userNumber && u.IsDelete != 1);
            if (null == user)
            {
                return new RespRet<Page<object>>(false, "用户名不存在");
            }
            string district = user.District;

            var options = new JsonSerializerOptions();
            options.PropertyNameCaseInsensitive = true; // 设置属性不区分大小写
            Xzq model = System.Text.Json.JsonSerializer.Deserialize<Xzq>(district, options);

            string strWhere = "";
            if (!string.IsNullOrEmpty(xzqdm))
            {
                if (user.RoleType == 1 || user.RoleType == 2)
                {
                    strWhere += "and xzqdm ='" + xzqdm + "'";
                }
                else if (user.RoleType == 3)
                {
                    strWhere += " and xzqdm='" + model.RegionName + "'";
                }
                else
                {
                    towndm = model.RegionCode;
                    if (model.RegionCode.Length != 12)
                    {
                        towndm = model.RegionCode + "000";
                    }
                    strWhere += " and town='" + towndm + "'";
                }
            }
            if (cked == "true")
            {
                strWhere += " and village in (select villagedm from zdcmd)  ";
            }
            if (string.IsNullOrEmpty(startTime) && string.IsNullOrEmpty(endTime))
            {
                startTime = DateTime.Now.ToShortDateString();
                endTime = DateTime.Now.ToShortDateString();
            }
            if (!string.IsNullOrEmpty(txtKeyWord) )
            {
                strWhere += " and (fwbh='" + txtKeyWord + "' or hzxm like '%"+ txtKeyWord + "%' OR address like '%"+ txtKeyWord + "%')";
            }
            if (!string.IsNullOrEmpty(xzqdm) && !string.IsNullOrEmpty(towndm) && !string.IsNullOrEmpty(villagedm))
            {
                strWhere += " and xzqdm='" + xzqdm + "' and town='" + towndm + "' and village='" + villagedm + "'";
            }
            else if (!string.IsNullOrEmpty(xzqdm) && !string.IsNullOrEmpty(towndm) && string.IsNullOrEmpty(villagedm))
            {
                strWhere += " and xzqdm='" + xzqdm + "' and town='" + towndm + "' ";
            }
            else if (!string.IsNullOrEmpty(xzqdm) && string.IsNullOrEmpty(towndm) && string.IsNullOrEmpty(villagedm))
            {
                strWhere += " and xzqdm='" + xzqdm + "' ";
            }

            string sql = "select  ROW_NUMBER() OVER ()index,* from (select * from (select ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num, t1.xzq,t1.xzqdm,t1.towndm,t1.town,t1.xzq,t1.villagedm,t1.village,t1.hzxm,t1.address,t1.fwbh,t1.cs,t1.jsrysstd,t1.create_on,t1.create_name,t1.yhly,case when t1.wdwhjgfjgqsyyrkrgbdj=1 then '是' when t1.wdwhjgfjgqsyyrkrgbdj='0' then '否' end wdwhjgfjgqsyyrkrgbdj,\r\ncase when t1.sstdsyyrkrclzx=1 then '是' when t1.sstdsyyrkrclzx='0' then '否' end sstdsyyrkrclzx,\r\ncase when t1.gryssdcsszblyyqksddp=1 then '是' when t1.gryssdcsszblyyqksddp='0' then '否' end gryssdcsszblyyqksddp,\r\ncase when t1.fwyfwdddzxccdsssfayqjshsf=1 then '是' when t1.fwyfwdddzxccdsssfayqjshsf='0' then '否' end fwyfwdddzxccdsssfayqjshsf,\r\ncase when use_electric_bicycle=1 then '是' when use_electric_bicycle='0' then '否' end use_electric_bicycle,\r\ncase when use_bottled_liquefied_gas=1 then '是' when use_bottled_liquefied_gas='0' then '否' end use_bottled_liquefied_gas,\r\ncase when use_stacked_items=1 then '是' when use_stacked_items='0' then '否' end use_stacked_items,\r\ncase when mhqwbcwhyx=1 then '是' when mhqwbcwhyx='0' then '否' end mhqwbcwhyx,\r\ncase when jyplmhxtwbcwhyx='1' then '是' when jyplmhxtwbcwhyx='0' then '否' end jyplmhxtwbcwhyx,\r\ncase when ygbjwfwhyx='1' then '是' when ygbjwfwhyx='0' then '否' end ygbjwfwhyx,\r\ncase when slljdx='1' then '是' when slljdx='0' then '否' end slljdx,\r\ncase when sfczckfhwggptzl='1' then '是' when sfczckfhwggptzl='0' then '否' end sfczckfhwggptzl,notes from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id=t2.base_id and t1.yhly='2' and t1.create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59' ) t" +
                "   where row_num=1 and yhly='2' and create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59'  " +
                "and fwbh not in(select fwbh from zm_pc_base_cj where fwzt is not null)  " +
                "and fwbh not in(select fwbh from zm_pc_base_sqjz where fwzt is not null and  yhly='4') " + strWhere + " order by create_on desc,xzqdm ,village asc ) t limit " + pagesize + " offset(" + pageindex + ") * " + pagesize;
            //string sql = "select ROW_NUMBER() OVER ()index,*,case when yhcount=0 and zgcount=0 then '100.00%' else  concat(        CASE            WHEN (round((((zgcount)::numeric / yhcount) * (100)::numeric), 2) > (100)::numeric) THEN 100.00            ELSE round((((zgcount)::numeric / yhcount) * (100)::numeric), 2)        END, '%') end zgl  from zcqkxztext('" + startTime+"','"+endTime+"') where 1 = 1   " + strWhere + " order by xzqdm,towndm asc  limit " + pagesize + " offset (" + pageindex + " - 1) * " + pagesize + "";
            if (sql.Contains("insert into", StringComparison.OrdinalIgnoreCase)
                || sql.Contains("delete from", StringComparison.OrdinalIgnoreCase)
                || sql.Contains("update ", StringComparison.OrdinalIgnoreCase))
            {
                return new RespRet<Page<object>>(false, "不符合规范，禁止执行");
            }
            string sql_count = "select  count(1) cnt from (select ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num, t1.xzq,t1.xzqdm,t1.towndm,t1.town,t1.xzq,t1.villagedm,t1.village,t1.hzxm,t1.address,t1.fwbh,t1.cs,t1.jsrysstd,t1.create_on,t1.create_name,t1.yhly,case when t1.wdwhjgfjgqsyyrkrgbdj=1 then '是' when t1.wdwhjgfjgqsyyrkrgbdj='0' then '否' end wdwhjgfjgqsyyrkrgbdj,\r\ncase when t1.sstdsyyrkrclzx=1 then '是' when t1.sstdsyyrkrclzx='0' then '否' end sstdsyyrkrclzx,\r\ncase when t1.gryssdcsszblyyqksddp=1 then '是' when t1.gryssdcsszblyyqksddp='0' then '否' end gryssdcsszblyyqksddp,\r\ncase when t1.fwyfwdddzxccdsssfayqjshsf=1 then '是' when t1.fwyfwdddzxccdsssfayqjshsf='0' then '否' end fwyfwdddzxccdsssfayqjshsf,\r\ncase when use_electric_bicycle=1 then '是' when use_electric_bicycle='0' then '否' end use_electric_bicycle,\r\ncase when use_bottled_liquefied_gas=1 then '是' when use_bottled_liquefied_gas='0' then '否' end use_bottled_liquefied_gas,\r\ncase when use_stacked_items=1 then '是' when use_stacked_items='0' then '否' end use_stacked_items,\r\ncase when mhqwbcwhyx=1 then '是' when mhqwbcwhyx='0' then '否' end mhqwbcwhyx,\r\ncase when jyplmhxtwbcwhyx='1' then '是' when jyplmhxtwbcwhyx='0' then '否' end jyplmhxtwbcwhyx,\r\ncase when ygbjwfwhyx='1' then '是' when ygbjwfwhyx='0' then '否' end ygbjwfwhyx,\r\ncase when slljdx='1' then '是' when slljdx='0' then '否' end slljdx,\r\ncase when sfczckfhwggptzl='1' then '是' when sfczckfhwggptzl='0' then '否' end sfczckfhwggptzl,notes " +
                "  from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id=t2.base_id and t1.yhly='2' and create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59' ) t where row_num=1 and  yhly='2'" +
                " and create_on BETWEEN '"+startTime+"' and '"+endTime+" 23:59:59' and fwbh not in(select fwbh from zm_pc_base_cj where fwzt is not null) " +
                " and fwbh not in(select fwbh from zm_pc_base_sqjz where fwzt is not null and  yhly='4') " + strWhere+" ";
         
            if (sql_count.Contains("insert into", StringComparison.OrdinalIgnoreCase)
                || sql_count.Contains("delete from", StringComparison.OrdinalIgnoreCase)
                || sql_count.Contains("update ", StringComparison.OrdinalIgnoreCase))
            {
                return new RespRet<Page<object>>(false, "不符合规范，禁止执行");
            }


            int count = _dbContext.Database.GetDbConnection().QueryFirstOrDefault<int>(sql_count);
            var datas = _dbContext.Database.GetDbConnection().Query<object>(sql).ToList();
            return new RespRet<Page<object>>(true, "ok", new Page<object> { Items = datas, NextPageIndex = pageindex + 1, PageTotal = count, Total = count });
        }



        /// <summary>
        /// 9项核验明细（区级\乡镇）
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpGet("housesqjhc/otherbaseqjYh")]
        [Produces("application/json")]
        public RespRet<Page<object>> otherbaseqjYh([FromQuery] string xzqdm, [FromQuery] string towndm, [FromQuery] string villagedm, [FromQuery] int pageindex, [FromQuery] int pagesize, [FromQuery] string startTime, [FromQuery] string endTime, [FromQuery] string txtKeyWord, [FromQuery] string yhly, [FromQuery] string cked, [FromQuery] string jsrysstd, [FromQuery] string wdwhjgfjgqsyyrkrgbdj, [FromQuery] string sfczckfhwggptzl, [FromQuery] string use_electric_bicycle, [FromQuery] string fwyfwdddzxccdsssfayqjshsf, [FromQuery] string use_bottled_liquefied_gas, [FromQuery] string use_stacked_items, [FromQuery] string slljdx, [FromQuery] string cs, [FromQuery] string nickname)
        {
            if (pageindex < 0)
            {
                return new RespRet<Page<object>>(false, "PageIndex must >= 0");
            }
            var userNumber = this.CurrentUser();
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<Page<object>>(false, "user not logined");
            }
            var user = _userRepository.FirstOrDefault(u => u.Number == userNumber && u.IsDelete != 1);
            if (null == user)
            {
                return new RespRet<Page<object>>(false, "用户名不存在");
            }
            string district = user.District;
            var options = new JsonSerializerOptions();
            options.PropertyNameCaseInsensitive = true; // 设置属性不区分大小写
            Xzq model = System.Text.Json.JsonSerializer.Deserialize<Xzq>(district, options);
            string strWhere = "";

            if (user.RoleType == 3)
            {
                strWhere += " and xzqdm='" + model.RegionCode + "'";
            }
            else if (user.RoleType == 3)
            {
                strWhere += " and xzqdm='" + model.RegionName + "'";
            }
            else if (user.RoleType == 4)
            {
                towndm = model.RegionCode;
                if (model.RegionCode.Length != 12)
                {
                    towndm = model.RegionCode + "000";
                }
                strWhere += " and town='" + towndm + "'";
            }

            if (string.IsNullOrEmpty(startTime) && string.IsNullOrEmpty(endTime))
            {
                startTime = DateTime.Now.ToShortDateString();
                endTime = DateTime.Now.ToShortDateString();
            }

            if (cked == "true")
            {
                strWhere += " and village in (select villagedm from zdcmd )  ";
            }

            if (!string.IsNullOrEmpty(txtKeyWord))
            {
                strWhere += " and (fwbh='" + txtKeyWord + "' or hzxm like '%" + txtKeyWord + "%' OR address like '%" + txtKeyWord + "%')";
            }
            if (!string.IsNullOrEmpty(xzqdm) && !string.IsNullOrEmpty(towndm) && !string.IsNullOrEmpty(villagedm))
            {
                strWhere += " and xzqdm='" + xzqdm + "' and town='" + towndm + "' and village='" + villagedm + "'";
            }
            else if (!string.IsNullOrEmpty(xzqdm) && !string.IsNullOrEmpty(towndm) && string.IsNullOrEmpty(villagedm))
            {
                strWhere += " and xzqdm='" + xzqdm + "' and town='" + towndm + "' ";
            }
            else if (!string.IsNullOrEmpty(xzqdm) && string.IsNullOrEmpty(towndm) && string.IsNullOrEmpty(villagedm))
            {
                strWhere += " and xzqdm='" + xzqdm + "' ";
            }
            if (!string.IsNullOrEmpty(nickname))
            {
                strWhere += " and create_name ='"+nickname+"'  ";
            }
            if (!string.IsNullOrEmpty(slljdx))
            {
                strWhere += " and slljdx ='"+ slljdx + "'  ";
            }
            if (!string.IsNullOrEmpty(use_stacked_items))
            {
                strWhere += " and use_stacked_items ='"+ use_stacked_items + "'  ";
            }
            if (!string.IsNullOrEmpty(use_bottled_liquefied_gas))
            {
                strWhere += " and use_bottled_liquefied_gas ='"+ use_bottled_liquefied_gas + "'  ";
            }
            if (!string.IsNullOrEmpty(fwyfwdddzxccdsssfayqjshsf))
            {
                strWhere += " and fwyfwdddzxccdsssfayqjshsf ='"+ fwyfwdddzxccdsssfayqjshsf + "' ";
            }
            if (!string.IsNullOrEmpty(use_electric_bicycle))
            {
                strWhere += " and use_electric_bicycle ='"+ use_electric_bicycle + "' ";
            }
            if (!string.IsNullOrEmpty(sfczckfhwggptzl))
            {
                strWhere += " and sfczckfhwggptzl='"+ sfczckfhwggptzl + "' ";
            }
            if (!string.IsNullOrEmpty(wdwhjgfjgqsyyrkrgbdj))
            {
                strWhere += " and wdwhjgfjgqsyyrkrgbdj ='"+ wdwhjgfjgqsyyrkrgbdj + "' ";
            }
            if (!string.IsNullOrEmpty(jsrysstd))
            {
                strWhere += " and jsrysstd='"+ jsrysstd + "'  ";
            }
            if (!string.IsNullOrEmpty(cs) && cs != "0")
            {
                strWhere += " and cs in (" + cs + ") ";
            }
            string today = DateTime.Now.ToString("yyyy-MM-dd");
            string table_fwzt = "";
            if (!string.IsNullOrEmpty(endTime))
            {
                DateTime endData = Convert.ToDateTime(endTime);
                table_fwzt = "zm_building_fwzt_" + endData.Year;
            }
            else
            {
                DateTime endData = Convert.ToDateTime(today);
                table_fwzt = "zm_building_fwzt_" + endData.Year;
            }
            string sql = "select  ROW_NUMBER() OVER ()index,* from (select * from (select ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num, t1.xzq,t1.xzqdm,t1.towndm,t1.town,t1.xzq,t1.villagedm,t1.village,t1.hzxm,t1.address,t1.fwbh,t1.cs,t1.jsrysstd,t1.create_on,t1.create_name,t1.yhly,case when t1.wdwhjgfjgqsyyrkrgbdj=1 then '是' when t1.wdwhjgfjgqsyyrkrgbdj='0' then '否' end wdwhjgfjgqsyyrkrgbdj,\r\ncase when t1.sstdsyyrkrclzx=1 then '是' when t1.sstdsyyrkrclzx='0' then '否' end sstdsyyrkrclzx,\r\ncase when t1.gryssdcsszblyyqksddp=1 then '是' when t1.gryssdcsszblyyqksddp='0' then '否' end gryssdcsszblyyqksddp,\r\ncase when t1.fwyfwdddzxccdsssfayqjshsf=1" +
                " then '是' when t1.fwyfwdddzxccdsssfayqjshsf='0' then '否' end fwyfwdddzxccdsssfayqjshsf,\r\ncase when use_electric_bicycle=1 then '是' when use_electric_bicycle='0' then '否' end use_electric_bicycle,\r\ncase when use_bottled_liquefied_gas=1 then '是' when use_bottled_liquefied_gas='0' then '否' end use_bottled_liquefied_gas,\r\ncase when use_stacked_items=1 then '是' when use_stacked_items='0' then '否' end use_stacked_items,\r\ncase when mhqwbcwhyx=1 then '是' when mhqwbcwhyx='0' then '否' end mhqwbcwhyx,\r\ncase when jyplmhxtwbcwhyx='1' then '是' when jyplmhxtwbcwhyx='0' then '否' end jyplmhxtwbcwhyx,\r\ncase when ygbjwfwhyx='1' then '是' when ygbjwfwhyx='0' " +
                "then '否' end ygbjwfwhyx,\r\ncase when slljdx='1' then '是' when slljdx='0' then '否' end slljdx,\r\ncase when sfczckfhwggptzl='1' then '是' when sfczckfhwggptzl='0' then '否' end sfczckfhwggptzl,notes from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id=t2.base_id and t1.yhly='"+yhly+ "' and t1.create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59') t" +
                "   where row_num=1 and yhly='"+ yhly + "' and create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59'  " +
                "and fwbh not in(select distinct fwbh from "+ table_fwzt + " where final_fwzt is not null and   final_create_on='"+endTime+" 23:59:59')  " +
                " " + strWhere + " order by create_on desc,xzqdm ,village asc ) t limit " + pagesize + " offset(" + pageindex + ") * " + pagesize;
            //string sql = "select ROW_NUMBER() OVER ()index,*,case when yhcount=0 and zgcount=0 then '100.00%' else  concat(        CASE            WHEN (round((((zgcount)::numeric / yhcount) * (100)::numeric), 2) > (100)::numeric) THEN 100.00            ELSE round((((zgcount)::numeric / yhcount) * (100)::numeric), 2)        END, '%') end zgl  from zcqkxztext('" + startTime+"','"+endTime+"') where 1 = 1   " + strWhere + " order by xzqdm,towndm asc  limit " + pagesize + " offset (" + pageindex + " - 1) * " + pagesize + "";
            if (sql.Contains("insert into", StringComparison.OrdinalIgnoreCase)
                || sql.Contains("delete from", StringComparison.OrdinalIgnoreCase)
                || sql.Contains("update ", StringComparison.OrdinalIgnoreCase))
            {
                return new RespRet<Page<object>>(false, "不符合规范，禁止执行");
            }
            string sql_count = "select  count(1) cnt from (select ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num, t1.xzq,t1.xzqdm,t1.towndm,t1.town,t1.xzq,t1.villagedm,t1.village,t1.hzxm,t1.address,t1.fwbh,t1.cs,t1.jsrysstd,t1.create_on,t1.create_name,t1.yhly,case when t1.wdwhjgfjgqsyyrkrgbdj=1 then '是' when t1.wdwhjgfjgqsyyrkrgbdj='0' then '否' end wdwhjgfjgqsyyrkrgbdj,\r\ncase when t1.sstdsyyrkrclzx=1 then '是' when t1.sstdsyyrkrclzx='0' then '否' end sstdsyyrkrclzx,\r\ncase when t1.gryssdcsszblyyqksddp=1 then '是' when t1.gryssdcsszblyyqksddp='0' then '否' end gryssdcsszblyyqksddp,\r\ncase when t1.fwyfwdddzxccdsssfayqjshsf=1 then " +
                "'是' when t1.fwyfwdddzxccdsssfayqjshsf='0' then '否' end fwyfwdddzxccdsssfayqjshsf,\r\ncase when use_electric_bicycle=1 then '是' when use_electric_bicycle='0' then '否' end use_electric_bicycle,\r\ncase when use_bottled_liquefied_gas=1 then '是' when use_bottled_liquefied_gas='0' then '否' end use_bottled_liquefied_gas,\r\ncase when use_stacked_items=1 then '是' when use_stacked_items='0' then '否' end use_stacked_items,\r\ncase when mhqwbcwhyx=1 then '是' when mhqwbcwhyx='0' then '否' end mhqwbcwhyx,\r\ncase when jyplmhxtwbcwhyx='1' then '是' when jyplmhxtwbcwhyx='0' then '否' end jyplmhxtwbcwhyx,\r\ncase when ygbjwfwhyx='1' then '是' when ygbjwfwhyx='0' " +
                "then '否' end ygbjwfwhyx,\r\ncase when slljdx='1' then '是' when slljdx='0' then '否' end slljdx,\r\ncase when sfczckfhwggptzl='1' then '是' when sfczckfhwggptzl='0' then '否' end sfczckfhwggptzl,notes " +
                "  from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id=t2.base_id and t1.yhly='"+ yhly + "'and t1.create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59') t where row_num=1 and  yhly='" + yhly+"'" +
                " and create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59' and fwbh not in(select distinct fwbh from "+ table_fwzt + " where final_fwzt is not null and   final_create_on='"+endTime+" 23:59:59') " +
                "  " + strWhere + " ";

            if (sql_count.Contains("insert into", StringComparison.OrdinalIgnoreCase)
                || sql_count.Contains("delete from", StringComparison.OrdinalIgnoreCase)
                || sql_count.Contains("update ", StringComparison.OrdinalIgnoreCase))
            {
                return new RespRet<Page<object>>(false, "不符合规范，禁止执行");
            }
            int count = _dbContext.Database.GetDbConnection().QueryFirstOrDefault<int>(sql_count);
            var datas = _dbContext.Database.GetDbConnection().Query<object>(sql).ToList();
            return new RespRet<Page<object>>(true, "ok", new Page<object> { Items = datas, NextPageIndex = pageindex + 1, PageTotal = count, Total = count });
        }

        /// <summary>
        /// 统计核验变化情况（区级）
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
       // [HttpGet("yhtj/yhtjlist")]
       // [Produces("application/json")]
       // public RespRet<Page<object>> yhtjlist([FromQuery] string xzqdm, [FromQuery] string towndm, [FromQuery] string villagedm, [FromQuery] int pageindex, [FromQuery] int pagesize, [FromQuery] string startTime, [FromQuery] string endTime, [FromQuery] string txtKeyWord, [FromQuery] string yhly)
       // {
       //     if (pageindex < 0)
       //     {
       //         return new RespRet<Page<object>>(false, "PageIndex must >= 0");
       //     }

       //     var userNumber = this.CurrentUser();
       //     if (string.IsNullOrEmpty(userNumber))
       //     {
       //         return new RespRet<Page<object>>(false, "user not logined");
       //     }
       //     var user = _userRepository.FirstOrDefault(u => u.Number == userNumber && u.IsDelete != 1);
       //     if (null == user)
       //     {
       //         return new RespRet<Page<object>>(false, "用户名不存在");
       //     }
       //     string district = user.District;

       //     var options = new JsonSerializerOptions();
       //     options.PropertyNameCaseInsensitive = true; // 设置属性不区分大小写
       //     Xzq model = System.Text.Json.JsonSerializer.Deserialize<Xzq>(district, options);

       //     string strWhere = "";
       //     if (!string.IsNullOrEmpty(xzqdm))
       //     {
       //         if (user.RoleType == 1 || user.RoleType == 2)
       //         {
       //             strWhere += "and xzqdm ='" + xzqdm + "'";
       //         }
       //         else if (user.RoleType == 3)
       //         {
       //             strWhere += " and xzqdm='" + model.RegionName + "'";
       //         }
       //         else
       //         {
       //             towndm = model.RegionCode;
       //             if (model.RegionCode.Length != 12)
       //             {
       //                 towndm = model.RegionCode + "000";
       //             }
       //             strWhere += " and town='" + towndm + "'";
       //         }
       //     }

       //     if (string.IsNullOrEmpty(startTime) && string.IsNullOrEmpty(endTime))
       //     {
       //         startTime = DateTime.Now.ToShortDateString();
       //         endTime = DateTime.Now.ToShortDateString();
       //     }
       //     if (!string.IsNullOrEmpty(txtKeyWord))
       //     {
       //         strWhere += " and (fwbh='" + txtKeyWord + "' or hzxm like '%" + txtKeyWord + "%' OR address like '%" + txtKeyWord + "%')";
       //     }
       //     if (!string.IsNullOrEmpty(xzqdm) && !string.IsNullOrEmpty(towndm) && !string.IsNullOrEmpty(villagedm))
       //     {
       //         strWhere += " and xzqdm='" + xzqdm + "' and town='" + towndm + "' and village='" + villagedm + "'";
       //     }
       //     else if (!string.IsNullOrEmpty(xzqdm) && !string.IsNullOrEmpty(towndm) && string.IsNullOrEmpty(villagedm))
       //     {
       //         strWhere += " and xzqdm='" + xzqdm + "' and town='" + towndm + "' ";
       //     }
       //     else if (!string.IsNullOrEmpty(xzqdm) && string.IsNullOrEmpty(towndm) && string.IsNullOrEmpty(villagedm))
       //     {
       //         strWhere += " and xzqdm='" + xzqdm + "' ";
       //     }

       //     if (string.IsNullOrEmpty(yhly))
       //     {
       //         yhly = "0";
       //     }
       //     string sql = @"select  ROW_NUMBER() OVER ()index,*  from (select  	k.xzq,k.xzqdm,COALESCE(cnt, 0) AS cnt,COALESCE(jsrysstd_s, 0) AS jsrysstd_s,COALESCE(jsrysstd_f, 0) AS jsrysstd_f,COALESCE(wdwhjgfjgqsyyrkrgbdj_s, 0) AS wdwhjgfjgqsyyrkrgbdj_s,COALESCE(wdwhjgfjgqsyyrkrgbdj_f, 0) AS wdwhjgfjgqsyyrkrgbdj_f,COALESCE(sfczckfhwggptzl_s, 0) AS sfczckfhwggptzl_s,	COALESCE(sfczckfhwggptzl_f, 0) AS sfczckfhwggptzl_f,COALESCE(use_electric_bicycle_s, 0) AS use_electric_bicycle_s,COALESCE(use_electric_bicycle_f, 0) AS use_electric_bicycle_f,COALESCE(fwyfwdddzxccdsssfayqjshsf_s, 0) AS fwyfwdddzxccdsssfayqjshsf_s,COALESCE(fwyfwdddzxccdsssfayqjshsf_f, 0) AS fwyfwdddzxccdsssfayqjshsf_f,COALESCE(use_bottled_liquefied_gas_s, 0) AS use_bottled_liquefied_gas_s,COALESCE(use_bottled_liquefied_gas_f, 0) AS use_bottled_liquefied_gas_f,COALESCE(use_stacked_items_s, 0) AS use_stacked_items_s,COALESCE(use_stacked_items_f, 0) AS use_stacked_items_f,
							//COALESCE(slljdx_s, 0) AS slljdx_s,COALESCE(slljdx_f, 0) AS slljdx_f,COALESCE(notes_s, 0) AS notes_s,COALESCE(notes_f, 0) AS notes_f from (select  xzqdm,xzq,count(1) cnt,
       //                     sum(case when jsrysstd= '是' OR jsrysstd is null then 1 else 0 end)  jsrysstd_s,
       //                     sum(case when jsrysstd= '否'  then 1 else 0 end)  jsrysstd_f,
       //                     sum(case when wdwhjgfjgqsyyrkrgbdj= '是'  OR wdwhjgfjgqsyyrkrgbdj is null  then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_s,
       //                     sum(case when wdwhjgfjgqsyyrkrgbdj= '否'then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_f,
       //                     sum(case when sfczckfhwggptzl= '是'  then 1 else 0 end)  sfczckfhwggptzl_s,
       //                     sum(case when sfczckfhwggptzl= '否'  then 1 else 0 end)  sfczckfhwggptzl_f,
       //                     sum(case when use_electric_bicycle= '是'  then 1 else 0 end) use_electric_bicycle_s,
       //                     sum(case when use_electric_bicycle= '否' then 1 else 0 end)  use_electric_bicycle_f,
       //                     sum(case when fwyfwdddzxccdsssfayqjshsf= '是' OR fwyfwdddzxccdsssfayqjshsf is null then 1 else 0 end) fwyfwdddzxccdsssfayqjshsf_s,
       //                     sum(case when fwyfwdddzxccdsssfayqjshsf= '否'  then 1 else 0 end)  fwyfwdddzxccdsssfayqjshsf_f,
       //                     sum(case when use_bottled_liquefied_gas= '是' then 1 else 0 end) use_bottled_liquefied_gas_s,
       //                     sum(case when use_bottled_liquefied_gas= '否' OR use_bottled_liquefied_gas is null then 1 else 0 end)  use_bottled_liquefied_gas_f,
       //                     sum(case when use_stacked_items= '是' then 1 else 0 end) use_stacked_items_s,
       //                     sum(case when use_stacked_items= '否' OR use_stacked_items is null then 1 else 0 end)  use_stacked_items_f,
       //                     sum(case when slljdx= '是' then 1 else 0 end) slljdx_s,
       //                     sum(case when slljdx= '否' OR slljdx is null then 1 else 0 end)  slljdx_f,
       //                     sum(case when notes is not null then 1 else 0 end) notes_s,
       //                     sum(case when notes is null then 1 else 0 end)  notes_f
       //                      from(select * from(select ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num, t1.xzq, t1.xzqdm, t1.towndm, t1.town, t1.villagedm, t1.village, t1.hzxm, t1.address, t1.fwbh, t1.cs, t1.jsrysstd, t1.create_on, t1.create_name, t1.yhly,case when t1.wdwhjgfjgqsyyrkrgbdj = 1 then '是' when t1.wdwhjgfjgqsyyrkrgbdj = '0' then '否' end wdwhjgfjgqsyyrkrgbdj,
       //                     case when t1.sstdsyyrkrclzx = 1 then '是' when t1.sstdsyyrkrclzx = '0' then '否' end sstdsyyrkrclzx,
       //                     case when t1.gryssdcsszblyyqksddp = 1 then '是' when t1.gryssdcsszblyyqksddp = '0' then '否' end gryssdcsszblyyqksddp,
       //                     case when t1.fwyfwdddzxccdsssfayqjshsf = 1 then '是' when t1.fwyfwdddzxccdsssfayqjshsf = '0' then '否' end fwyfwdddzxccdsssfayqjshsf,
       //                     case when use_electric_bicycle= 1 then '是' when use_electric_bicycle = '0' then '否' end use_electric_bicycle,
       //                     case when use_bottled_liquefied_gas= 1 then '是' when use_bottled_liquefied_gas = '0' then '否' end use_bottled_liquefied_gas,
       //                     case when use_stacked_items= 1 then '是' when use_stacked_items = '0' then '否' end use_stacked_items,
       //                     case when mhqwbcwhyx= 1 then '是' when mhqwbcwhyx = '0' then '否' end mhqwbcwhyx,
       //                     case when jyplmhxtwbcwhyx= '1' then '是' when jyplmhxtwbcwhyx = '0' then '否' end jyplmhxtwbcwhyx,
       //                     case when ygbjwfwhyx= '1' then '是' when ygbjwfwhyx = '0' then '否' end ygbjwfwhyx,
       //                     case when slljdx= '1' then '是' when slljdx = '0' then '否' end slljdx,
       //                     case when sfczckfhwggptzl= '1' then '是' when sfczckfhwggptzl = '0' then '否' end sfczckfhwggptzl, notes from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id = t2.base_id and t1.yhly='" + yhly + "' ) t " +
       //                    " where row_num = 1 and yhly = '"+yhly+"' and create_on BETWEEN '"+startTime+"' and '"+endTime+" 23:59:59'  and fwbh not in(select fwbh from zm_pc_base_cj where fwzt is not null)  "+
       //                    " and fwbh not in(select fwbh from zm_pc_base_sqjz where fwzt is not null and yhly = '4')  order by create_on desc, xzqdm, village asc ) t group by xzq, xzqdm order by xzqdm) t right join (select DISTINCT xzq,xzqdm from zm_building_ledger) k on t.xzqdm=k.xzqdm  where  1=1 ) t  where  1=1 " + strWhere + " order by xzqdm";
           
       //     if (sql.Contains("insert into", StringComparison.OrdinalIgnoreCase)
       //         || sql.Contains("delete from", StringComparison.OrdinalIgnoreCase)
       //         || sql.Contains("update ", StringComparison.OrdinalIgnoreCase))
       //         {
       //             return new RespRet<Page<object>>(false, "不符合规范，禁止执行");
       //         }
       //     string sql_count = @"select  count(1) cnt  from (select  	k.xzq,k.xzqdm,COALESCE(cnt, 0) AS cnt,COALESCE(jsrysstd_s, 0) AS jsrysstd_s,COALESCE(jsrysstd_f, 0) AS jsrysstd_f,COALESCE(wdwhjgfjgqsyyrkrgbdj_s, 0) AS wdwhjgfjgqsyyrkrgbdj_s,COALESCE(wdwhjgfjgqsyyrkrgbdj_f, 0) AS wdwhjgfjgqsyyrkrgbdj_f,COALESCE(sfczckfhwggptzl_s, 0) AS sfczckfhwggptzl_s,	COALESCE(sfczckfhwggptzl_f, 0) AS sfczckfhwggptzl_f,COALESCE(use_electric_bicycle_s, 0) AS use_electric_bicycle_s,COALESCE(use_electric_bicycle_f, 0) AS use_electric_bicycle_f,COALESCE(fwyfwdddzxccdsssfayqjshsf_s, 0) AS fwyfwdddzxccdsssfayqjshsf_s,COALESCE(fwyfwdddzxccdsssfayqjshsf_f, 0) AS fwyfwdddzxccdsssfayqjshsf_f,COALESCE(use_bottled_liquefied_gas_s, 0) AS use_bottled_liquefied_gas_s,COALESCE(use_bottled_liquefied_gas_f, 0) AS use_bottled_liquefied_gas_f,COALESCE(use_stacked_items_s, 0) AS use_stacked_items_s,COALESCE(use_stacked_items_f, 0) AS use_stacked_items_f,
							//COALESCE(slljdx_s, 0) AS slljdx_s,COALESCE(slljdx_f, 0) AS slljdx_f,COALESCE(notes_s, 0) AS notes_s,COALESCE(notes_f, 0) AS notes_f from (select  xzqdm,xzq,count(1) cnt,
       //                     sum(case when jsrysstd= '是' OR jsrysstd is null then 1 else 0 end)  jsrysstd_s,
       //                     sum(case when jsrysstd= '否'  then 1 else 0 end)  jsrysstd_f,
       //                     sum(case when wdwhjgfjgqsyyrkrgbdj= '是' OR wdwhjgfjgqsyyrkrgbdj is null then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_s,
       //                     sum(case when wdwhjgfjgqsyyrkrgbdj= '否'  then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_f,
       //                     sum(case when sfczckfhwggptzl= '是' OR sfczckfhwggptzl is null then 1 else 0 end)  sfczckfhwggptzl_s,
       //                     sum(case when sfczckfhwggptzl= '否'  then 1 else 0 end)  sfczckfhwggptzl_f,
       //                     sum(case when use_electric_bicycle= '是' then 1 else 0 end) use_electric_bicycle_s,
       //                     sum(case when use_electric_bicycle= '否' OR use_electric_bicycle is null then 1 else 0 end)  use_electric_bicycle_f,
       //                     sum(case when fwyfwdddzxccdsssfayqjshsf= '是' OR fwyfwdddzxccdsssfayqjshsf is null then 1 else 0 end) fwyfwdddzxccdsssfayqjshsf_s,
       //                     sum(case when fwyfwdddzxccdsssfayqjshsf= '否'  then 1 else 0 end)  fwyfwdddzxccdsssfayqjshsf_f,
       //                     sum(case when use_bottled_liquefied_gas= '是' then 1 else 0 end) use_bottled_liquefied_gas_s,
       //                     sum(case when use_bottled_liquefied_gas= '否' OR use_bottled_liquefied_gas is null then 1 else 0 end)  use_bottled_liquefied_gas_f,
       //                     sum(case when use_stacked_items= '是' then 1 else 0 end) use_stacked_items_s,
       //                     sum(case when use_stacked_items= '否' OR use_stacked_items is null then 1 else 0 end)  use_stacked_items_f,
       //                     sum(case when slljdx= '是' then 1 else 0 end) slljdx_s,
       //                     sum(case when slljdx= '否' OR slljdx is null then 1 else 0 end)  slljdx_f,
       //                     sum(case when notes is not null then 1 else 0 end) notes_s,
       //                     sum(case when notes is null then 1 else 0 end)  notes_f
       //                      from(select * from(select ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num, t1.xzq, t1.xzqdm, t1.towndm, t1.town, t1.villagedm, t1.village, t1.hzxm, t1.address, t1.fwbh, t1.cs, t1.jsrysstd, t1.create_on, t1.create_name, t1.yhly,case when t1.wdwhjgfjgqsyyrkrgbdj = 1 then '是' when t1.wdwhjgfjgqsyyrkrgbdj = '0' then '否' end wdwhjgfjgqsyyrkrgbdj,
       //                     case when t1.sstdsyyrkrclzx = 1 then '是' when t1.sstdsyyrkrclzx = '0' then '否' end sstdsyyrkrclzx,
       //                     case when t1.gryssdcsszblyyqksddp = 1 then '是' when t1.gryssdcsszblyyqksddp = '0' then '否' end gryssdcsszblyyqksddp,
       //                     case when t1.fwyfwdddzxccdsssfayqjshsf = 1 then '是' when t1.fwyfwdddzxccdsssfayqjshsf = '0' then '否' end fwyfwdddzxccdsssfayqjshsf,
       //                     case when use_electric_bicycle= 1 then '是' when use_electric_bicycle = '0' then '否' end use_electric_bicycle,
       //                     case when use_bottled_liquefied_gas= 1 then '是' when use_bottled_liquefied_gas = '0' then '否' end use_bottled_liquefied_gas,
       //                     case when use_stacked_items= 1 then '是' when use_stacked_items = '0' then '否' end use_stacked_items,
       //                     case when mhqwbcwhyx= 1 then '是' when mhqwbcwhyx = '0' then '否' end mhqwbcwhyx,
       //                     case when jyplmhxtwbcwhyx= '1' then '是' when jyplmhxtwbcwhyx = '0' then '否' end jyplmhxtwbcwhyx,
       //                     case when ygbjwfwhyx= '1' then '是' when ygbjwfwhyx = '0' then '否' end ygbjwfwhyx,
       //                     case when slljdx= '1' then '是' when slljdx = '0' then '否' end slljdx,
       //                     case when sfczckfhwggptzl= '1' then '是' when sfczckfhwggptzl = '0' then '否' end sfczckfhwggptzl, notes from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id = t2.base_id and t1.yhly='"+yhly+"' ) t " +
       //                  " where row_num = 1 and yhly = '" + yhly + "' and create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59'  and fwbh not in(select fwbh from zm_pc_base_cj where fwzt is not null)  " +
       //                  " and fwbh not in(select fwbh from zm_pc_base_sqjz where fwzt is not null and yhly = '4')  order by create_on desc, xzqdm, village asc ) t group by xzq, xzqdm order by xzqdm) t right join (select DISTINCT xzq,xzqdm from zm_building_ledger) k on t.xzqdm=k.xzqdm  where  1=1 ) t  where  1=1 " + strWhere + "";
       //     if (sql_count.Contains("insert into", StringComparison.OrdinalIgnoreCase)
       //         || sql_count.Contains("delete from", StringComparison.OrdinalIgnoreCase)
       //         || sql_count.Contains("update ", StringComparison.OrdinalIgnoreCase))
       //     {
       //         return new RespRet<Page<object>>(false, "不符合规范，禁止执行");
       //     }


       //     int count = _dbContext.Database.GetDbConnection().QueryFirstOrDefault<int>(sql_count);
       //     var datas = _dbContext.Database.GetDbConnection().Query<object>(sql).ToList();
       //     return new RespRet<Page<object>>(true, "ok", new Page<object> { Items = datas, NextPageIndex = pageindex + 1, PageTotal = count, Total = count });
       // }

       // /// <summary>
       // /// 统计核验变化情况（区级）
       // /// </summary>
       // /// <param name="pageRequest"></param>
       // /// <returns></returns>
       // [HttpGet("yhtj/yhtjtownlist")]
       // [Produces("application/json")]
       // public RespRet<Page<object>> yhtjtownlist([FromQuery] string xzqdm, [FromQuery] string towndm, [FromQuery] string villagedm, [FromQuery] int pageindex, [FromQuery] int pagesize, [FromQuery] string startTime, [FromQuery] string endTime, [FromQuery] string txtKeyWord, [FromQuery] string yhly)
       // {
       //     if (pageindex < 0)
       //     {
       //         return new RespRet<Page<object>>(false, "PageIndex must >= 0");
       //     }

       //     var userNumber = this.CurrentUser();
       //     if (string.IsNullOrEmpty(userNumber))
       //     {
       //         return new RespRet<Page<object>>(false, "user not logined");
       //     }
       //     var user = _userRepository.FirstOrDefault(u => u.Number == userNumber && u.IsDelete != 1);
       //     if (null == user)
       //     {
       //         return new RespRet<Page<object>>(false, "用户名不存在");
       //     }
       //     string district = user.District;

       //     var options = new JsonSerializerOptions();
       //     options.PropertyNameCaseInsensitive = true; // 设置属性不区分大小写
       //     Xzq model = System.Text.Json.JsonSerializer.Deserialize<Xzq>(district, options);

       //     string strWhere = "";
       //     if (!string.IsNullOrEmpty(xzqdm))
       //     {
       //         if (user.RoleType == 1 || user.RoleType == 2)
       //         {
       //             strWhere += "and xzqdm ='" + xzqdm + "'";
       //         }
       //         else if (user.RoleType == 3)
       //         {
       //             strWhere += " and xzqdm='" + model.RegionName + "'";
       //         }
       //         else
       //         {
       //             towndm = model.RegionCode;
       //             if (model.RegionCode.Length != 12)
       //             {
       //                 towndm = model.RegionCode + "000";
       //             }
       //             strWhere += " and town='" + towndm + "'";
       //         }
       //     }

       //     if (string.IsNullOrEmpty(startTime) && string.IsNullOrEmpty(endTime))
       //     {
       //         startTime = DateTime.Now.ToShortDateString();
       //         endTime = DateTime.Now.ToShortDateString();
       //     }
       //     if (!string.IsNullOrEmpty(txtKeyWord))
       //     {
       //         strWhere += " and (fwbh='" + txtKeyWord + "' or hzxm like '%" + txtKeyWord + "%' OR address like '%" + txtKeyWord + "%')";
       //     }
       //     if (!string.IsNullOrEmpty(xzqdm) && !string.IsNullOrEmpty(towndm) && !string.IsNullOrEmpty(villagedm))
       //     {
       //         strWhere += " and xzqdm='" + xzqdm + "' and town='" + towndm + "' and village='" + villagedm + "'";
       //     }
       //     else if (!string.IsNullOrEmpty(xzqdm) && !string.IsNullOrEmpty(towndm) && string.IsNullOrEmpty(villagedm))
       //     {
       //         strWhere += " and xzqdm='" + xzqdm + "' and town='" + towndm + "' ";
       //     }
       //     else if (!string.IsNullOrEmpty(xzqdm) && string.IsNullOrEmpty(towndm) && string.IsNullOrEmpty(villagedm))
       //     {
       //         strWhere += " and xzqdm='" + xzqdm + "' ";
       //     }
       //     if (string.IsNullOrEmpty(yhly)) 
       //     {
       //         yhly = "0";
       //     }

       //     string sql = @"select  ROW_NUMBER() OVER ()index,*  from (select  k.xzq,k.xzqdm,k.town,k.towndm ,COALESCE(cnt, 0) AS cnt,COALESCE(jsrysstd_s, 0) AS jsrysstd_s,COALESCE(jsrysstd_f, 0) AS jsrysstd_f,COALESCE(wdwhjgfjgqsyyrkrgbdj_s, 0) AS wdwhjgfjgqsyyrkrgbdj_s,COALESCE(wdwhjgfjgqsyyrkrgbdj_f, 0) AS wdwhjgfjgqsyyrkrgbdj_f,COALESCE(sfczckfhwggptzl_s, 0) AS sfczckfhwggptzl_s,	COALESCE(sfczckfhwggptzl_f, 0) AS sfczckfhwggptzl_f,COALESCE(use_electric_bicycle_s, 0) AS use_electric_bicycle_s,COALESCE(use_electric_bicycle_f, 0) AS use_electric_bicycle_f,COALESCE(fwyfwdddzxccdsssfayqjshsf_s, 0) AS fwyfwdddzxccdsssfayqjshsf_s,COALESCE(fwyfwdddzxccdsssfayqjshsf_f, 0) AS fwyfwdddzxccdsssfayqjshsf_f,COALESCE(use_bottled_liquefied_gas_s, 0) AS use_bottled_liquefied_gas_s,COALESCE(use_bottled_liquefied_gas_f, 0) AS use_bottled_liquefied_gas_f,COALESCE(use_stacked_items_s, 0) AS use_stacked_items_s,COALESCE(use_stacked_items_f, 0) AS use_stacked_items_f,
							//COALESCE(slljdx_s, 0) AS slljdx_s,COALESCE(slljdx_f, 0) AS slljdx_f,COALESCE(notes_s, 0) AS notes_s,COALESCE(notes_f, 0) AS notes_f from (select t.xzqdm,t.xzq,t.town towndm,m.town ,count(1) cnt,
       //                     sum(case when jsrysstd= '是'  OR jsrysstd is null then 1 else 0 end)  jsrysstd_s,
       //                     sum(case when jsrysstd= '否' then 1 else 0 end)  jsrysstd_f,
       //                     sum(case when wdwhjgfjgqsyyrkrgbdj= '是' OR wdwhjgfjgqsyyrkrgbdj is null then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_s,
       //                     sum(case when wdwhjgfjgqsyyrkrgbdj= '否'  then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_f,
       //                     sum(case when sfczckfhwggptzl= '是' OR sfczckfhwggptzl is null then 1 else 0 end)  sfczckfhwggptzl_s,
       //                     sum(case when sfczckfhwggptzl= '否'  then 1 else 0 end)  sfczckfhwggptzl_f,
       //                     sum(case when use_electric_bicycle= '是' OR use_electric_bicycle is null then 1 else 0 end) use_electric_bicycle_s,
       //                     sum(case when use_electric_bicycle= '否'  then 1 else 0 end)  use_electric_bicycle_f,
       //                     sum(case when fwyfwdddzxccdsssfayqjshsf= '是'  OR fwyfwdddzxccdsssfayqjshsf is null then 1 else 0 end) fwyfwdddzxccdsssfayqjshsf_s,
       //                     sum(case when fwyfwdddzxccdsssfayqjshsf= '否' then 1 else 0 end)  fwyfwdddzxccdsssfayqjshsf_f,
       //                     sum(case when use_bottled_liquefied_gas= '是' OR use_bottled_liquefied_gas is null  then 1 else 0 end) use_bottled_liquefied_gas_s,
       //                     sum(case when use_bottled_liquefied_gas= '否' then 1 else 0 end)  use_bottled_liquefied_gas_f,
       //                     sum(case when use_stacked_items= '是' OR use_stacked_items is null then 1 else 0 end) use_stacked_items_s,
       //                     sum(case when use_stacked_items= '否'  then 1 else 0 end)  use_stacked_items_f,
       //                     sum(case when slljdx= '是' OR slljdx is null then 1 else 0 end) slljdx_s,
       //                     sum(case when slljdx= '否'  then 1 else 0 end)  slljdx_f,
       //                     sum(case when notes is not null then 1 else 0 end) notes_s,
       //                     sum(case when notes is null then 1 else 0 end)  notes_f
       //                      from(select * from(select ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num, t1.xzq, t1.xzqdm, t1.towndm, t1.town, t1.villagedm, t1.village, t1.hzxm, t1.address, t1.fwbh, t1.cs, t1.jsrysstd, t1.create_on, t1.create_name, t1.yhly,case when t1.wdwhjgfjgqsyyrkrgbdj = 1 then '是' when t1.wdwhjgfjgqsyyrkrgbdj = '0' then '否' end wdwhjgfjgqsyyrkrgbdj,
       //                     case when t1.sstdsyyrkrclzx = 1 then '是' when t1.sstdsyyrkrclzx = '0' then '否' end sstdsyyrkrclzx,
       //                     case when t1.gryssdcsszblyyqksddp = 1 then '是' when t1.gryssdcsszblyyqksddp = '0' then '否' end gryssdcsszblyyqksddp,
       //                     case when t1.fwyfwdddzxccdsssfayqjshsf = 1 then '是' when t1.fwyfwdddzxccdsssfayqjshsf = '0' then '否' end fwyfwdddzxccdsssfayqjshsf,
       //                     case when use_electric_bicycle= 1 then '是' when use_electric_bicycle = '0' then '否' end use_electric_bicycle,
       //                     case when use_bottled_liquefied_gas= 1 then '是' when use_bottled_liquefied_gas = '0' then '否' end use_bottled_liquefied_gas,
       //                     case when use_stacked_items= 1 then '是' when use_stacked_items = '0' then '否' end use_stacked_items,
       //                     case when mhqwbcwhyx= 1 then '是' when mhqwbcwhyx = '0' then '否' end mhqwbcwhyx,
       //                     case when jyplmhxtwbcwhyx= '1' then '是' when jyplmhxtwbcwhyx = '0' then '否' end jyplmhxtwbcwhyx,
       //                     case when ygbjwfwhyx= '1' then '是' when ygbjwfwhyx = '0' then '否' end ygbjwfwhyx,
       //                     case when slljdx= '1' then '是' when slljdx = '0' then '否' end slljdx,
       //                     case when sfczckfhwggptzl= '1' then '是' when sfczckfhwggptzl = '0' then '否' end sfczckfhwggptzl, notes from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id = t2.base_id and t1.yhly='" + yhly+"') t " +
       //                    " where row_num = 1 and yhly = '" + yhly + "' and create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59'  and fwbh not in(select fwbh from zm_pc_base_cj where fwzt is not null)  " +
       //                    " and fwbh not in(select fwbh from zm_pc_base_sqjz where fwzt is not null and yhly = '4')  order by create_on desc, xzqdm, village asc ) t left join zm_building_ledger m on t.town=m.towndm  group by t.xzq,t.xzqdm,t.town,m.town order by t.town) t right join (select DISTINCT xzq,xzqdm,town,towndm from zm_building_ledger where xzqdm='" + xzqdm+"' ) k on t.xzqdm=k.xzqdm  where  1=1 ) t where  1=1 " + strWhere;

       //     if (sql.Contains("insert into", StringComparison.OrdinalIgnoreCase)
       //         || sql.Contains("delete from", StringComparison.OrdinalIgnoreCase)
       //         || sql.Contains("update ", StringComparison.OrdinalIgnoreCase))
       //     {
       //         return new RespRet<Page<object>>(false, "不符合规范，禁止执行");
       //     }
       //     string sql_count = @"select  count(1)cnt  from (select  k.xzq,k.xzqdm,k.town,k.towndm ,COALESCE(cnt, 0) AS cnt,COALESCE(jsrysstd_s, 0) AS jsrysstd_s,COALESCE(jsrysstd_f, 0) AS jsrysstd_f,COALESCE(wdwhjgfjgqsyyrkrgbdj_s, 0) AS wdwhjgfjgqsyyrkrgbdj_s,COALESCE(wdwhjgfjgqsyyrkrgbdj_f, 0) AS wdwhjgfjgqsyyrkrgbdj_f,COALESCE(sfczckfhwggptzl_s, 0) AS sfczckfhwggptzl_s,	COALESCE(sfczckfhwggptzl_f, 0) AS sfczckfhwggptzl_f,COALESCE(use_electric_bicycle_s, 0) AS use_electric_bicycle_s,COALESCE(use_electric_bicycle_f, 0) AS use_electric_bicycle_f,COALESCE(fwyfwdddzxccdsssfayqjshsf_s, 0) AS fwyfwdddzxccdsssfayqjshsf_s,COALESCE(fwyfwdddzxccdsssfayqjshsf_f, 0) AS fwyfwdddzxccdsssfayqjshsf_f,COALESCE(use_bottled_liquefied_gas_s, 0) AS use_bottled_liquefied_gas_s,COALESCE(use_bottled_liquefied_gas_f, 0) AS use_bottled_liquefied_gas_f,COALESCE(use_stacked_items_s, 0) AS use_stacked_items_s,COALESCE(use_stacked_items_f, 0) AS use_stacked_items_f,
							//COALESCE(slljdx_s, 0) AS slljdx_s,COALESCE(slljdx_f, 0) AS slljdx_f,COALESCE(notes_s, 0) AS notes_s,COALESCE(notes_f, 0) AS notes_f from (select t.xzqdm,t.xzq,t.town towndm,m.town ,count(1) cnt,
       //                     sum(case when jsrysstd= '是' OR jsrysstd is null then 1 else 0 end)  jsrysstd_s,
       //                     sum(case when jsrysstd= '否'  then 1 else 0 end)  jsrysstd_f,
       //                     sum(case when wdwhjgfjgqsyyrkrgbdj= '是'  OR wdwhjgfjgqsyyrkrgbdj is null then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_s,
       //                     sum(case when wdwhjgfjgqsyyrkrgbdj= '否' then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_f,
       //                     sum(case when sfczckfhwggptzl= '是'  OR sfczckfhwggptzl is null  then 1 else 0 end)  sfczckfhwggptzl_s,
       //                     sum(case when sfczckfhwggptzl= '否'then 1 else 0 end)  sfczckfhwggptzl_f,
       //                     sum(case when use_electric_bicycle= '是' OR use_electric_bicycle is null  then 1 else 0 end) use_electric_bicycle_s,
       //                     sum(case when use_electric_bicycle= '否' then 1 else 0 end)  use_electric_bicycle_f,
       //                     sum(case when fwyfwdddzxccdsssfayqjshsf= '是' OR fwyfwdddzxccdsssfayqjshsf is null then 1 else 0 end) fwyfwdddzxccdsssfayqjshsf_s,
       //                     sum(case when fwyfwdddzxccdsssfayqjshsf= '否'  then 1 else 0 end)  fwyfwdddzxccdsssfayqjshsf_f,
       //                     sum(case when use_bottled_liquefied_gas= '是' OR use_bottled_liquefied_gas is null then 1 else 0 end) use_bottled_liquefied_gas_s,
       //                     sum(case when use_bottled_liquefied_gas= '否'  then 1 else 0 end)  use_bottled_liquefied_gas_f,
       //                     sum(case when use_stacked_items= '是'  OR use_stacked_items is null then 1 else 0 end) use_stacked_items_s,
       //                     sum(case when use_stacked_items= '否' then 1 else 0 end)  use_stacked_items_f,
       //                     sum(case when slljdx= '是' OR slljdx is null then 1 else 0 end) slljdx_s,
       //                     sum(case when slljdx= '否'  then 1 else 0 end)  slljdx_f,
       //                     sum(case when notes is not null then 1 else 0 end) notes_s,
       //                     sum(case when notes is null then 1 else 0 end)  notes_f
       //                      from(select * from(select ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num, t1.xzq, t1.xzqdm, t1.towndm, t1.town, t1.villagedm, t1.village, t1.hzxm, t1.address, t1.fwbh, t1.cs, t1.jsrysstd, t1.create_on, t1.create_name, t1.yhly,case when t1.wdwhjgfjgqsyyrkrgbdj = 1 then '是' when t1.wdwhjgfjgqsyyrkrgbdj = '0' then '否' end wdwhjgfjgqsyyrkrgbdj,
       //                     case when t1.sstdsyyrkrclzx = 1 then '是' when t1.sstdsyyrkrclzx = '0' then '否' end sstdsyyrkrclzx,
       //                     case when t1.gryssdcsszblyyqksddp = 1 then '是' when t1.gryssdcsszblyyqksddp = '0' then '否' end gryssdcsszblyyqksddp,
       //                     case when t1.fwyfwdddzxccdsssfayqjshsf = 1 then '是' when t1.fwyfwdddzxccdsssfayqjshsf = '0' then '否' end fwyfwdddzxccdsssfayqjshsf,
       //                     case when use_electric_bicycle= 1 then '是' when use_electric_bicycle = '0' then '否' end use_electric_bicycle,
       //                     case when use_bottled_liquefied_gas= 1 then '是' when use_bottled_liquefied_gas = '0' then '否' end use_bottled_liquefied_gas,
       //                     case when use_stacked_items= 1 then '是' when use_stacked_items = '0' then '否' end use_stacked_items,
       //                     case when mhqwbcwhyx= 1 then '是' when mhqwbcwhyx = '0' then '否' end mhqwbcwhyx,
       //                     case when jyplmhxtwbcwhyx= '1' then '是' when jyplmhxtwbcwhyx = '0' then '否' end jyplmhxtwbcwhyx,
       //                     case when ygbjwfwhyx= '1' then '是' when ygbjwfwhyx = '0' then '否' end ygbjwfwhyx,
       //                     case when slljdx= '1' then '是' when slljdx = '0' then '否' end slljdx,
       //                     case when sfczckfhwggptzl= '1' then '是' when sfczckfhwggptzl = '0' then '否' end sfczckfhwggptzl, notes from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id = t2.base_id and t1.yhly='" + yhly+"' ) t " +
       //                    " where row_num = 1 and yhly = '" + yhly + "' and create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59'  and fwbh not in(select fwbh from zm_pc_base_cj where fwzt is not null)  " +
       //                    " and fwbh not in(select fwbh from zm_pc_base_sqjz where fwzt is not null and yhly = '4')  order by create_on desc, xzqdm, village asc ) t left join zm_building_ledger m on t.town=m.towndm  group by t.xzq,t.xzqdm,t.town,m.town order by t.town) t right join (select DISTINCT xzq,xzqdm,town,towndm from zm_building_ledger where xzqdm='" + xzqdm + "' ) k on t.xzqdm=k.xzqdm  where  1=1 ) t where  1=1 " + strWhere;
       //     if (sql_count.Contains("insert into", StringComparison.OrdinalIgnoreCase)
       //         || sql_count.Contains("delete from", StringComparison.OrdinalIgnoreCase)
       //         || sql_count.Contains("update ", StringComparison.OrdinalIgnoreCase))
       //     {
       //         return new RespRet<Page<object>>(false, "不符合规范，禁止执行");
       //     }


       //     int count = _dbContext.Database.GetDbConnection().QueryFirstOrDefault<int>(sql_count);
       //     var datas = _dbContext.Database.GetDbConnection().Query<object>(sql).ToList();
       //     return new RespRet<Page<object>>(true, "ok", new Page<object> { Items = datas, NextPageIndex = pageindex + 1, PageTotal = count, Total = count });
       // }


       // /// <summary>
       // /// 统计核验变化情况（区级）
       // /// </summary>
       // /// <param name="pageRequest"></param>
       // /// <returns></returns>
       // [HttpGet("yhtj/yhtjvillagelist")]
       // [Produces("application/json")]
       // public RespRet<Page<object>> yhtjvillagelist([FromQuery] string xzqdm, [FromQuery] string towndm, [FromQuery] string villagedm, [FromQuery] int pageindex, [FromQuery] int pagesize, [FromQuery] string startTime, [FromQuery] string endTime, [FromQuery] string txtKeyWord, [FromQuery] string yhly)
       // {
       //     if (pageindex < 0)
       //     {
       //         return new RespRet<Page<object>>(false, "PageIndex must >= 0");
       //     }

       //     var userNumber = this.CurrentUser();
       //     if (string.IsNullOrEmpty(userNumber))
       //     {
       //         return new RespRet<Page<object>>(false, "user not logined");
       //     }
       //     var user = _userRepository.FirstOrDefault(u => u.Number == userNumber && u.IsDelete != 1);
       //     if (null == user)
       //     {
       //         return new RespRet<Page<object>>(false, "用户名不存在");
       //     }
       //     string district = user.District;

       //     var options = new JsonSerializerOptions();
       //     options.PropertyNameCaseInsensitive = true; // 设置属性不区分大小写
       //     Xzq model = System.Text.Json.JsonSerializer.Deserialize<Xzq>(district, options);

       //     string strWhere = "";
       //     if (!string.IsNullOrEmpty(xzqdm))
       //     {
       //         if (user.RoleType == 1 || user.RoleType == 2)
       //         {
       //             strWhere += "and xzqdm ='" + xzqdm + "'";
       //         }
       //         else if (user.RoleType == 3)
       //         {
       //             strWhere += " and xzqdm='" + model.RegionName + "'";
       //         }
       //         else
       //         {
       //             towndm = model.RegionCode;
       //             if (model.RegionCode.Length != 12)
       //             {
       //                 towndm = model.RegionCode + "000";
       //             }
       //             strWhere += " and town='" + towndm + "'";
       //         }
       //     }

       //     if (string.IsNullOrEmpty(startTime) && string.IsNullOrEmpty(endTime))
       //     {
       //         startTime = DateTime.Now.ToShortDateString();
       //         endTime = DateTime.Now.ToShortDateString();
       //     }
       //     if (!string.IsNullOrEmpty(txtKeyWord))
       //     {
       //         strWhere += " and (fwbh='" + txtKeyWord + "' or hzxm like '%" + txtKeyWord + "%' OR address like '%" + txtKeyWord + "%')";
       //     }
       //     if (!string.IsNullOrEmpty(xzqdm) && !string.IsNullOrEmpty(towndm) && !string.IsNullOrEmpty(villagedm))
       //     {
       //         strWhere += " and xzqdm='" + xzqdm + "' and towndm='" + towndm + "' and villagedm='" + villagedm + "'";
       //     }
       //     else if (!string.IsNullOrEmpty(xzqdm) && !string.IsNullOrEmpty(towndm) && string.IsNullOrEmpty(villagedm))
       //     {
       //         strWhere += " and xzqdm='" + xzqdm + "' and towndm='" + towndm + "' ";
       //     }
       //     else if (!string.IsNullOrEmpty(xzqdm) && string.IsNullOrEmpty(towndm) && string.IsNullOrEmpty(villagedm))
       //     {
       //         strWhere += " and xzqdm='" + xzqdm + "' ";
       //     }
       //     if (string.IsNullOrEmpty(yhly)) 
       //     {
       //         yhly = "0";
       //     }

       //     string sql = @"select  ROW_NUMBER() OVER ()index,*  from (select  	k.xzq,k.xzqdm,k.town,k.towndm,k.village,k.villagedm, COALESCE(cnt, 0) AS cnt,COALESCE(jsrysstd_s, 0) AS jsrysstd_s,COALESCE(jsrysstd_f, 0) AS jsrysstd_f,COALESCE(wdwhjgfjgqsyyrkrgbdj_s, 0) AS wdwhjgfjgqsyyrkrgbdj_s,COALESCE(wdwhjgfjgqsyyrkrgbdj_f, 0) AS wdwhjgfjgqsyyrkrgbdj_f,COALESCE(sfczckfhwggptzl_s, 0) AS sfczckfhwggptzl_s,	COALESCE(sfczckfhwggptzl_f, 0) AS sfczckfhwggptzl_f,COALESCE(use_electric_bicycle_s, 0) AS use_electric_bicycle_s,COALESCE(use_electric_bicycle_f, 0) AS use_electric_bicycle_f,COALESCE(fwyfwdddzxccdsssfayqjshsf_s, 0) AS fwyfwdddzxccdsssfayqjshsf_s,COALESCE(fwyfwdddzxccdsssfayqjshsf_f, 0) AS fwyfwdddzxccdsssfayqjshsf_f,COALESCE(use_bottled_liquefied_gas_s, 0) AS use_bottled_liquefied_gas_s,COALESCE(use_bottled_liquefied_gas_f, 0) AS use_bottled_liquefied_gas_f,COALESCE(use_stacked_items_s, 0) AS use_stacked_items_s,COALESCE(use_stacked_items_f, 0) AS use_stacked_items_f,
							//COALESCE(slljdx_s, 0) AS slljdx_s,COALESCE(slljdx_f, 0) AS slljdx_f,COALESCE(notes_s, 0) AS notes_s,COALESCE(notes_f, 0) AS notes_f from (select t.xzqdm,t.xzq,t.town towndm,m.town ,t.village villagedm,k.villagedm, count(1) cnt,
       //                     sum(case when jsrysstd= '是'  OR jsrysstd is null  then 1 else 0 end)  jsrysstd_s,
       //                     sum(case when jsrysstd= '否'then 1 else 0 end)  jsrysstd_f,
       //                     sum(case when wdwhjgfjgqsyyrkrgbdj= '是'  OR wdwhjgfjgqsyyrkrgbdj is null  then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_s,
       //                     sum(case when wdwhjgfjgqsyyrkrgbdj= '否'then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_f,
       //                     sum(case when sfczckfhwggptzl= '是' OR sfczckfhwggptzl is null  then 1 else 0 end)  sfczckfhwggptzl_s,
       //                     sum(case when sfczckfhwggptzl= '否' then 1 else 0 end)  sfczckfhwggptzl_f,
       //                     sum(case when use_electric_bicycle= '是'  OR use_electric_bicycle is null  then 1 else 0 end) use_electric_bicycle_s,
       //                     sum(case when use_electric_bicycle= '否'then 1 else 0 end)  use_electric_bicycle_f,
       //                     sum(case when fwyfwdddzxccdsssfayqjshsf= '是'  OR fwyfwdddzxccdsssfayqjshsf is null  then 1 else 0 end) fwyfwdddzxccdsssfayqjshsf_s,
       //                     sum(case when fwyfwdddzxccdsssfayqjshsf= '否'then 1 else 0 end)  fwyfwdddzxccdsssfayqjshsf_f,
       //                     sum(case when use_bottled_liquefied_gas= '是' OR use_bottled_liquefied_gas is null then 1 else 0 end) use_bottled_liquefied_gas_s,
       //                     sum(case when use_bottled_liquefied_gas= '否'  then 1 else 0 end)  use_bottled_liquefied_gas_f,
       //                     sum(case when use_stacked_items= '是'  OR use_stacked_items is null  then 1 else 0 end) use_stacked_items_s,
       //                     sum(case when use_stacked_items= '否'then 1 else 0 end)  use_stacked_items_f,
       //                     sum(case when slljdx= '是' OR slljdx is null then 1 else 0 end) slljdx_s,
       //                     sum(case when slljdx= '否'  then 1 else 0 end)  slljdx_f,
       //                     sum(case when notes is not null then 1 else 0 end) notes_s,
       //                     sum(case when notes is null then 1 else 0 end)  notes_f
       //                      from(select * from(select ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num, t1.xzq, t1.xzqdm, t1.towndm, t1.town, t1.villagedm, t1.village, t1.hzxm, t1.address, t1.fwbh, t1.cs, t1.jsrysstd, t1.create_on, t1.create_name, t1.yhly,case when t1.wdwhjgfjgqsyyrkrgbdj = 1 then '是' when t1.wdwhjgfjgqsyyrkrgbdj = '0' then '否' end wdwhjgfjgqsyyrkrgbdj,
       //                     case when t1.sstdsyyrkrclzx = 1 then '是' when t1.sstdsyyrkrclzx = '0' then '否' end sstdsyyrkrclzx,
       //                     case when t1.gryssdcsszblyyqksddp = 1 then '是' when t1.gryssdcsszblyyqksddp = '0' then '否' end gryssdcsszblyyqksddp,
       //                     case when t1.fwyfwdddzxccdsssfayqjshsf = 1 then '是' when t1.fwyfwdddzxccdsssfayqjshsf = '0' then '否' end fwyfwdddzxccdsssfayqjshsf,
       //                     case when use_electric_bicycle= 1 then '是' when use_electric_bicycle = '0' then '否' end use_electric_bicycle,
       //                     case when use_bottled_liquefied_gas= 1 then '是' when use_bottled_liquefied_gas = '0' then '否' end use_bottled_liquefied_gas,
       //                     case when use_stacked_items= 1 then '是' when use_stacked_items = '0' then '否' end use_stacked_items,
       //                     case when mhqwbcwhyx= 1 then '是' when mhqwbcwhyx = '0' then '否' end mhqwbcwhyx,
       //                     case when jyplmhxtwbcwhyx= '1' then '是' when jyplmhxtwbcwhyx = '0' then '否' end jyplmhxtwbcwhyx,
       //                     case when ygbjwfwhyx= '1' then '是' when ygbjwfwhyx = '0' then '否' end ygbjwfwhyx,
       //                     case when slljdx= '1' then '是' when slljdx = '0' then '否' end slljdx,
       //                     case when sfczckfhwggptzl= '1' then '是' when sfczckfhwggptzl = '0' then '否' end sfczckfhwggptzl, notes from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id = t2.base_id  and t1.yhly='" + yhly+"') t " +
       //                    " where row_num = 1 and yhly = '" + yhly + "' and create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59'  and fwbh not in(select fwbh from zm_pc_base_cj where fwzt is not null)  " +
       //                    " and fwbh not in(select fwbh from zm_pc_base_sqjz where fwzt is not null and yhly = '4')  order by create_on desc, xzqdm, village asc ) t " +
       //                    "left join zm_building_ledger m on t.town=m.towndm left join zm_building_ledger k on t.village=k.villagedm  group by t.xzq,t.xzqdm,t.town,m.town,t.village,k.villagedm order by t.town,t.village) t right join (select DISTINCT xzq,xzqdm,town,towndm,village,villagedm from zm_building_ledger where xzqdm='" + xzqdm + "' and towndm='" + towndm+"' ) k on t.xzqdm=k.xzqdm  where  1=1 ) t where  1=1 " + strWhere;

       //     if (sql.Contains("insert into", StringComparison.OrdinalIgnoreCase)
       //         || sql.Contains("delete from", StringComparison.OrdinalIgnoreCase)
       //         || sql.Contains("update ", StringComparison.OrdinalIgnoreCase))
       //     {
       //         return new RespRet<Page<object>>(false, "不符合规范，禁止执行");
       //     }
       //     string sql_count = @"select  count(1) cnt  from (select  	k.xzq,k.xzqdm,k.town,k.towndm,k.village,k.villagedm, COALESCE(cnt, 0) AS cnt,COALESCE(jsrysstd_s, 0) AS jsrysstd_s,COALESCE(jsrysstd_f, 0) AS jsrysstd_f,COALESCE(wdwhjgfjgqsyyrkrgbdj_s, 0) AS wdwhjgfjgqsyyrkrgbdj_s,COALESCE(wdwhjgfjgqsyyrkrgbdj_f, 0) AS wdwhjgfjgqsyyrkrgbdj_f,COALESCE(sfczckfhwggptzl_s, 0) AS sfczckfhwggptzl_s,	COALESCE(sfczckfhwggptzl_f, 0) AS sfczckfhwggptzl_f,COALESCE(use_electric_bicycle_s, 0) AS use_electric_bicycle_s,COALESCE(use_electric_bicycle_f, 0) AS use_electric_bicycle_f,COALESCE(fwyfwdddzxccdsssfayqjshsf_s, 0) AS fwyfwdddzxccdsssfayqjshsf_s,COALESCE(fwyfwdddzxccdsssfayqjshsf_f, 0) AS fwyfwdddzxccdsssfayqjshsf_f,COALESCE(use_bottled_liquefied_gas_s, 0) AS use_bottled_liquefied_gas_s,COALESCE(use_bottled_liquefied_gas_f, 0) AS use_bottled_liquefied_gas_f,COALESCE(use_stacked_items_s, 0) AS use_stacked_items_s,COALESCE(use_stacked_items_f, 0) AS use_stacked_items_f,
							//COALESCE(slljdx_s, 0) AS slljdx_s,COALESCE(slljdx_f, 0) AS slljdx_f,COALESCE(notes_s, 0) AS notes_s,COALESCE(notes_f, 0) AS notes_f from (select t.xzqdm,t.xzq,t.town towndm,m.town ,t.village villagedm,k.villagedm, count(1) cnt,
       //                     sum(case when jsrysstd= '是' OR jsrysstd is null then 1 else 0 end)  jsrysstd_s,
       //                     sum(case when jsrysstd= '否'  then 1 else 0 end)  jsrysstd_f,
       //                     sum(case when wdwhjgfjgqsyyrkrgbdj= '是' OR wdwhjgfjgqsyyrkrgbdj is null then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_s,
       //                     sum(case when wdwhjgfjgqsyyrkrgbdj= '否'  then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_f,
       //                     sum(case when sfczckfhwggptzl= '是' OR sfczckfhwggptzl is null then 1 else 0 end)  sfczckfhwggptzl_s,
       //                     sum(case when sfczckfhwggptzl= '否'  then 1 else 0 end)  sfczckfhwggptzl_f,
       //                     sum(case when use_electric_bicycle= '是' OR use_electric_bicycle is null then 1 else 0 end) use_electric_bicycle_s,
       //                     sum(case when use_electric_bicycle= '否'  then 1 else 0 end)  use_electric_bicycle_f,
       //                     sum(case when fwyfwdddzxccdsssfayqjshsf= '是' OR fwyfwdddzxccdsssfayqjshsf is null then 1 else 0 end) fwyfwdddzxccdsssfayqjshsf_s,
       //                     sum(case when fwyfwdddzxccdsssfayqjshsf= '否'  then 1 else 0 end)  fwyfwdddzxccdsssfayqjshsf_f,
       //                     sum(case when use_bottled_liquefied_gas= '是' OR use_bottled_liquefied_gas is null then 1 else 0 end) use_bottled_liquefied_gas_s,
       //                     sum(case when use_bottled_liquefied_gas= '否'  then 1 else 0 end)  use_bottled_liquefied_gas_f,
       //                     sum(case when use_stacked_items= '是'  OR use_stacked_items is null then 1 else 0 end) use_stacked_items_s,
       //                     sum(case when use_stacked_items= '否' then 1 else 0 end)  use_stacked_items_f,
       //                     sum(case when slljdx= '是' OR slljdx is null  then 1 else 0 end) slljdx_s,
       //                     sum(case when slljdx= '否' then 1 else 0 end)  slljdx_f,
       //                     sum(case when notes is not null then 1 else 0 end) notes_s,
       //                     sum(case when notes is null then 1 else 0 end)  notes_f
       //                      from(select * from(select ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num, t1.xzq, t1.xzqdm, t1.towndm, t1.town, t1.villagedm, t1.village, t1.hzxm, t1.address, t1.fwbh, t1.cs, t1.jsrysstd, t1.create_on, t1.create_name, t1.yhly,case when t1.wdwhjgfjgqsyyrkrgbdj = 1 then '是' when t1.wdwhjgfjgqsyyrkrgbdj = '0' then '否' end wdwhjgfjgqsyyrkrgbdj,
       //                     case when t1.sstdsyyrkrclzx = 1 then '是' when t1.sstdsyyrkrclzx = '0' then '否' end sstdsyyrkrclzx,
       //                     case when t1.gryssdcsszblyyqksddp = 1 then '是' when t1.gryssdcsszblyyqksddp = '0' then '否' end gryssdcsszblyyqksddp,
       //                     case when t1.fwyfwdddzxccdsssfayqjshsf = 1 then '是' when t1.fwyfwdddzxccdsssfayqjshsf = '0' then '否' end fwyfwdddzxccdsssfayqjshsf,
       //                     case when use_electric_bicycle= 1 then '是' when use_electric_bicycle = '0' then '否' end use_electric_bicycle,
       //                     case when use_bottled_liquefied_gas= 1 then '是' when use_bottled_liquefied_gas = '0' then '否' end use_bottled_liquefied_gas,
       //                     case when use_stacked_items= 1 then '是' when use_stacked_items = '0' then '否' end use_stacked_items,
       //                     case when mhqwbcwhyx= 1 then '是' when mhqwbcwhyx = '0' then '否' end mhqwbcwhyx,
       //                     case when jyplmhxtwbcwhyx= '1' then '是' when jyplmhxtwbcwhyx = '0' then '否' end jyplmhxtwbcwhyx,
       //                     case when ygbjwfwhyx= '1' then '是' when ygbjwfwhyx = '0' then '否' end ygbjwfwhyx,
       //                     case when slljdx= '1' then '是' when slljdx = '0' then '否' end slljdx,
       //                     case when sfczckfhwggptzl= '1' then '是' when sfczckfhwggptzl = '0' then '否' end sfczckfhwggptzl, notes from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id = t2.base_id and t1.yhly='" + yhly+"' ) t " +
       //               " where row_num = 1 and yhly = '" + yhly + "' and create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59'  and fwbh not in(select fwbh from zm_pc_base_cj where fwzt is not null)  " +
       //               " and fwbh not in(select fwbh from zm_pc_base_sqjz where fwzt is not null and yhly = '4')  order by create_on desc, xzqdm, village asc ) t " +
       //               "left join zm_building_ledger m on t.town=m.towndm left join zm_building_ledger k on t.village=k.villagedm  group by t.xzq,t.xzqdm,t.town,m.town,t.village,k.villagedm order by t.town,t.village) t  right join (select DISTINCT xzq,xzqdm,town,towndm,village,villagedm from zm_building_ledger where xzqdm='" + xzqdm+"' and towndm='"+towndm+"' ) k on t.xzqdm=k.xzqdm  where  1=1 ) t where  1=1 " + strWhere;
       //     if (sql_count.Contains("insert into", StringComparison.OrdinalIgnoreCase)
       //         || sql_count.Contains("delete from", StringComparison.OrdinalIgnoreCase)
       //         || sql_count.Contains("update ", StringComparison.OrdinalIgnoreCase))
       //     {
       //         return new RespRet<Page<object>>(false, "不符合规范，禁止执行");
       //     }


       //     int count = _dbContext.Database.GetDbConnection().QueryFirstOrDefault<int>(sql_count);
       //     var datas = _dbContext.Database.GetDbConnection().Query<object>(sql).ToList();
       //     return new RespRet<Page<object>>(true, "ok", new Page<object> { Items = datas, NextPageIndex = pageindex + 1, PageTotal = count, Total = count });
       // }







        /// <summary>
        /// 统计核验变化情况（区级）
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpGet("yhtj/yhtjlist")]
        [Produces("application/json")]
        public RespRet<Page<object>> yhtjlistbydate([FromQuery] string xzqdm, [FromQuery] string towndm, [FromQuery] string villagedm, [FromQuery] int pageindex, [FromQuery] int pagesize, [FromQuery] string startTime, [FromQuery] string endTime, [FromQuery] string txtKeyWord, [FromQuery] string yhly)
        {
            if (pageindex < 0)
            {
                return new RespRet<Page<object>>(false, "PageIndex must >= 0");
            }

            var userNumber = this.CurrentUser();
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<Page<object>>(false, "user not logined");
            }
            var user = _userRepository.FirstOrDefault(u => u.Number == userNumber && u.IsDelete != 1);
            if (null == user)
            {
                return new RespRet<Page<object>>(false, "用户名不存在");
            }
            string district = user.District;

            var options = new JsonSerializerOptions();
            options.PropertyNameCaseInsensitive = true; // 设置属性不区分大小写
            Xzq model = System.Text.Json.JsonSerializer.Deserialize<Xzq>(district, options);

            string strWhere = "";
            if (!string.IsNullOrEmpty(xzqdm))
            {
                if (user.RoleType == 1 || user.RoleType == 2)
                {
                    strWhere += "and xzqdm ='" + xzqdm + "'";
                }
                else if (user.RoleType == 3)
                {
                    strWhere += " and xzqdm='" + model.RegionCode + "'";
                }
                else
                {
                    towndm = model.RegionCode;
                    if (model.RegionCode.Length != 12)
                    {
                        towndm = model.RegionCode + "000";
                    }
                    strWhere += " and town='" + towndm + "'";
                }
            }
            else
            {
                if (user.RoleType == 3)
                {
                    strWhere += " and xzqdm='" + model.RegionCode + "'";
                }

            }

            if (string.IsNullOrEmpty(startTime) && string.IsNullOrEmpty(endTime))
            {
                startTime = DateTime.Now.ToShortDateString();
                endTime = DateTime.Now.ToShortDateString();
            }
            if (!string.IsNullOrEmpty(txtKeyWord))
            {
                strWhere += " and (fwbh='" + txtKeyWord + "' or hzxm like '%" + txtKeyWord + "%' OR address like '%" + txtKeyWord + "%')";
            }
            if (!string.IsNullOrEmpty(xzqdm) && !string.IsNullOrEmpty(towndm) && !string.IsNullOrEmpty(villagedm))
            {
                strWhere += " and xzqdm='" + xzqdm + "' and town='" + towndm + "' and village='" + villagedm + "'";
            }
            else if (!string.IsNullOrEmpty(xzqdm) && !string.IsNullOrEmpty(towndm) && string.IsNullOrEmpty(villagedm))
            {
                strWhere += " and xzqdm='" + xzqdm + "' and town='" + towndm + "' ";
            }
            else if (!string.IsNullOrEmpty(xzqdm) && string.IsNullOrEmpty(towndm) && string.IsNullOrEmpty(villagedm))
            {
                strWhere += " and xzqdm='" + xzqdm + "' ";
            }

            if (string.IsNullOrEmpty(yhly))
            {
                yhly = "0";
            }
            string toDay = DateTime.Now.ToString("yyyy-MM-dd");
            string table_fwzt = "";
            if (!string.IsNullOrEmpty(endTime))
            {
                DateTime endData = Convert.ToDateTime(endTime);
                table_fwzt = "zm_building_fwzt_" + endData.Year;
            }
            else
            {
                DateTime endData = Convert.ToDateTime(toDay);
                table_fwzt = "zm_building_fwzt_" + endData.Year;
            }
            string sql = @$"  select  ROW_NUMBER() OVER ()index,*  from (select t.*,
                 COALESCE(tt.use_electric_bicycle_wzg, 0) use_electric_bicycle_wzg,
                 COALESCE(tt.use_electric_bicycle_yzg, 0) use_electric_bicycle_yzg,
                 COALESCE(tt.use_bottled_liquefied_gas_wzg, 0) use_bottled_liquefied_gas_wzg,
                 COALESCE(tt.use_bottled_liquefied_gas_yzg, 0) use_bottled_liquefied_gas_yzg,
                 COALESCE(tt.use_stacked_items_wzg, 0) use_stacked_items_wzg,
                 COALESCE(tt.use_stacked_items_yzg, 0) use_stacked_items_yzg,
                 COALESCE(tt.mhqwbcwhyx_wzg, 0) mhqwbcwhyx_wzg,
                 COALESCE(tt.mhqwbcwhyx_yzg, 0) mhqwbcwhyx_yzg,
                 COALESCE(tt.jyplmhxtwbcwhyx_wzg, 0) jyplmhxtwbcwhyx_wzg,
                 COALESCE(tt.jyplmhxtwbcwhyx_yzg, 0) jyplmhxtwbcwhyx_yzg,
                 COALESCE(tt.ygbjwfwhyx_wzg, 0) ygbjwfwhyx_wzg,
                 COALESCE(tt.ygbjwfwhyx_yzg, 0) ygbjwfwhyx_yzg,
                 COALESCE(tt.slljdx_wzg, 0) slljdx_wzg,
                 COALESCE(tt.slljdx_yzg, 0) slljdx_yzg,
                 COALESCE(tt.sfczckfhwggptzl_wzg, 0) sfczckfhwggptzl_wzg,
                 COALESCE(tt.sfczckfhwggptzl_yzg, 0) sfczckfhwggptzl_yzg 
                 from (select  k.xzq,k.xzqdm,COALESCE(cnt, 0) AS cnt,COALESCE(jsrysstd_s, 0) AS jsrysstd_s,COALESCE(jsrysstd_f, 0) AS jsrysstd_f,COALESCE(wdwhjgfjgqsyyrkrgbdj_s, 0) AS wdwhjgfjgqsyyrkrgbdj_s,COALESCE(wdwhjgfjgqsyyrkrgbdj_f, 0) AS wdwhjgfjgqsyyrkrgbdj_f,COALESCE(sfczckfhwggptzl_s, 0) AS sfczckfhwggptzl_s,	COALESCE(sfczckfhwggptzl_f, 0) AS sfczckfhwggptzl_f,COALESCE(use_electric_bicycle_s, 0) AS use_electric_bicycle_s,COALESCE(use_electric_bicycle_f, 0) AS use_electric_bicycle_f,COALESCE(fwyfwdddzxccdsssfayqjshsf_s, 0) AS fwyfwdddzxccdsssfayqjshsf_s,COALESCE(fwyfwdddzxccdsssfayqjshsf_f, 0) AS fwyfwdddzxccdsssfayqjshsf_f,COALESCE(use_bottled_liquefied_gas_s, 0) AS use_bottled_liquefied_gas_s,COALESCE(use_bottled_liquefied_gas_f, 0) AS use_bottled_liquefied_gas_f,COALESCE(use_stacked_items_s, 0) AS use_stacked_items_s,COALESCE(use_stacked_items_f, 0) AS use_stacked_items_f,
				COALESCE(slljdx_s, 0) AS slljdx_s,COALESCE(slljdx_f, 0) AS slljdx_f,COALESCE(notes_s, 0) AS notes_s,COALESCE(notes_f, 0) AS notes_f from (select  xzqdm,xzq,count(1) cnt,
                            sum(case when jsrysstd= '是'  then 1 else 0 end)  jsrysstd_s,
                            sum(case when jsrysstd= '否'OR jsrysstd is null  then 1 else 0 end)  jsrysstd_f,
                            sum(case when wdwhjgfjgqsyyrkrgbdj= '是'  then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_s,
                            sum(case when wdwhjgfjgqsyyrkrgbdj= '否' OR wdwhjgfjgqsyyrkrgbdj  is null  then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_f,
                            sum(case when sfczckfhwggptzl= '是'  then 1 else 0 end)  sfczckfhwggptzl_s,
                            sum(case when sfczckfhwggptzl= '否'  then 1 else 0 end)  sfczckfhwggptzl_f,
                            sum(case when use_electric_bicycle= '是'  then 1 else 0 end) use_electric_bicycle_s,
                            sum(case when use_electric_bicycle= '否' then 1 else 0 end)  use_electric_bicycle_f,
                            sum(case when fwyfwdddzxccdsssfayqjshsf= '是'  then 1 else 0 end) fwyfwdddzxccdsssfayqjshsf_s,
                            sum(case when fwyfwdddzxccdsssfayqjshsf= '否' OR fwyfwdddzxccdsssfayqjshsf is null then 1 else 0 end)  fwyfwdddzxccdsssfayqjshsf_f,
                            sum(case when use_bottled_liquefied_gas= '是' then 1 else 0 end) use_bottled_liquefied_gas_s,
                            sum(case when use_bottled_liquefied_gas= '否' OR use_bottled_liquefied_gas is null then 1 else 0 end)  use_bottled_liquefied_gas_f,
                            sum(case when use_stacked_items= '是' then 1 else 0 end) use_stacked_items_s,
                            sum(case when use_stacked_items= '否' OR use_stacked_items is null then 1 else 0 end)  use_stacked_items_f,
                            sum(case when slljdx= '是' then 1 else 0 end) slljdx_s,
                            sum(case when slljdx= '否' OR slljdx is null then 1 else 0 end)  slljdx_f,
                            sum(case when notes is not null then 1 else 0 end) notes_s,
                            sum(case when notes is null then 1 else 0 end)  notes_f
                             from(select * from(select ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num, t1.xzq, t1.xzqdm, t1.towndm, t1.town, t1.villagedm, t1.village, t1.hzxm, t1.address, t1.fwbh, t1.cs, t1.jsrysstd, t1.create_on, t1.create_name, t1.yhly,case when t1.wdwhjgfjgqsyyrkrgbdj = 1 then '是' when t1.wdwhjgfjgqsyyrkrgbdj = '0' then '否' end wdwhjgfjgqsyyrkrgbdj,
                            case when t1.sstdsyyrkrclzx = 1 then '是' when t1.sstdsyyrkrclzx = '0' then '否' end sstdsyyrkrclzx,
                            case when t1.gryssdcsszblyyqksddp = 1 then '是' when t1.gryssdcsszblyyqksddp = '0' then '否' end gryssdcsszblyyqksddp,
                            case when t1.fwyfwdddzxccdsssfayqjshsf = 1 then '是' when t1.fwyfwdddzxccdsssfayqjshsf = '0' then '否' end fwyfwdddzxccdsssfayqjshsf,
                            case when use_electric_bicycle= 1 then '是' when use_electric_bicycle = '0' then '否' end use_electric_bicycle,
                            case when use_bottled_liquefied_gas= 1 then '是' when use_bottled_liquefied_gas = '0' then '否' end use_bottled_liquefied_gas,
                            case when use_stacked_items= 1 then '是' when use_stacked_items = '0' then '否' end use_stacked_items,
                            case when mhqwbcwhyx= 1 then '是' when mhqwbcwhyx = '0' then '否' end mhqwbcwhyx,
                            case when jyplmhxtwbcwhyx= '1' then '是' when jyplmhxtwbcwhyx = '0' then '否' end jyplmhxtwbcwhyx,
                            case when ygbjwfwhyx= '1' then '是' when ygbjwfwhyx = '0' then '否' end ygbjwfwhyx,
                            case when slljdx= '1' then '是' when slljdx = '0' then '否' end slljdx,
                            case when sfczckfhwggptzl= '1' then '是' when sfczckfhwggptzl = '0' then '否' end sfczckfhwggptzl, notes from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id = t2.base_id and t1.yhly='"+ yhly + "' and t1.create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59' ) t "+
                           " where row_num =1 and yhly ='"+ yhly + "' and create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59'  and fwbh not in(select distinct fwbh from "+table_fwzt+"  where final_fwzt is not null and   final_create_on='"+endTime+" 23:59:59')  order by create_on desc, xzqdm, village asc ) t group by xzq, xzqdm order by xzqdm) t right join (select DISTINCT xzq,xzqdm from zm_building_ledger) k on t.xzqdm=k.xzqdm  where  1=1 ) t left join (select  a.xzqdm," +
  "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='use_electric_bicycle' then 1 end) use_electric_bicycle_wzg," +
  "count(case when fh_zt=0 and a.check_id='use_electric_bicycle' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)use_electric_bicycle_yzg , " +
  "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='use_bottled_liquefied_gas' then 1 end) use_bottled_liquefied_gas_wzg," +
  "count(case when fh_zt=0 and a.check_id='use_bottled_liquefied_gas' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)use_bottled_liquefied_gas_yzg," +
  "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='use_stacked_items' then 1 end) use_stacked_items_wzg," +
  "count(case when fh_zt=0 and a.check_id='use_stacked_items' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)use_stacked_items_yzg," +
  " count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='mhqwbcwhyx' then 1 end) mhqwbcwhyx_wzg," +
  " count(case when fh_zt=0 and a.check_id='mhqwbcwhyx' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP   then 1 end)mhqwbcwhyx_yzg," +
  " count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='jyplmhxtwbcwhyx' then 1 end)jyplmhxtwbcwhyx_wzg," +
  " count(case when fh_zt=0 and a.check_id='jyplmhxtwbcwhyx' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)jyplmhxtwbcwhyx_yzg," +
   "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='ygbjwfwhyx' then 1 end)ygbjwfwhyx_wzg," +
   "count(case when fh_zt=0 and a.check_id='ygbjwfwhyx' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)ygbjwfwhyx_yzg," +
   "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='slljdx' then 1 end)slljdx_wzg," +
   "count(case when fh_zt=0 and a.check_id='slljdx' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)slljdx_yzg," +
   "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='sfczckfhwggptzl' then 1 end)sfczckfhwggptzl_wzg," +
   "count(case when fh_zt=0 and a.check_id='sfczckfhwggptzl' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)sfczckfhwggptzl_yzg" +
   " from zm_pc_yhlog_dx_sqjz a  where a.jcly='" + yhly + "' and a.create_on::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  and a.fwbh not in(select distinct fwbh from "+table_fwzt+"  where final_fwzt is not null and   final_create_on='"+endTime+" 23:59:59')  	GROUP BY " +
   " a.xzqdm)tt on t.xzqdm=tt.xzqdm)t  where  1=1 " + strWhere + " order by xzqdm ";

            if (sql.Contains("insert into", StringComparison.OrdinalIgnoreCase)
                || sql.Contains("delete from", StringComparison.OrdinalIgnoreCase)
                || sql.Contains("update ", StringComparison.OrdinalIgnoreCase))
            {
                return new RespRet<Page<object>>(false, "不符合规范，禁止执行");
            }
            string sql_count = @"select  count(1) cnt  from (select  	k.xzq,k.xzqdm,COALESCE(cnt, 0) AS cnt,COALESCE(jsrysstd_s, 0) AS jsrysstd_s,COALESCE(jsrysstd_f, 0) AS jsrysstd_f,COALESCE(wdwhjgfjgqsyyrkrgbdj_s, 0) AS wdwhjgfjgqsyyrkrgbdj_s,COALESCE(wdwhjgfjgqsyyrkrgbdj_f, 0) AS wdwhjgfjgqsyyrkrgbdj_f,COALESCE(sfczckfhwggptzl_s, 0) AS sfczckfhwggptzl_s,	COALESCE(sfczckfhwggptzl_f, 0) AS sfczckfhwggptzl_f,COALESCE(use_electric_bicycle_s, 0) AS use_electric_bicycle_s,COALESCE(use_electric_bicycle_f, 0) AS use_electric_bicycle_f,COALESCE(fwyfwdddzxccdsssfayqjshsf_s, 0) AS fwyfwdddzxccdsssfayqjshsf_s,COALESCE(fwyfwdddzxccdsssfayqjshsf_f, 0) AS fwyfwdddzxccdsssfayqjshsf_f,COALESCE(use_bottled_liquefied_gas_s, 0) AS use_bottled_liquefied_gas_s,COALESCE(use_bottled_liquefied_gas_f, 0) AS use_bottled_liquefied_gas_f,COALESCE(use_stacked_items_s, 0) AS use_stacked_items_s,COALESCE(use_stacked_items_f, 0) AS use_stacked_items_f,
							COALESCE(slljdx_s, 0) AS slljdx_s,COALESCE(slljdx_f, 0) AS slljdx_f,COALESCE(notes_s, 0) AS notes_s,COALESCE(notes_f, 0) AS notes_f from (select  xzqdm,xzq,count(1) cnt,
                            sum(case when jsrysstd= '是' then 1 else 0 end)  jsrysstd_s,
                            sum(case when jsrysstd= '否' OR jsrysstd is null   then 1 else 0 end)  jsrysstd_f,
                            sum(case when wdwhjgfjgqsyyrkrgbdj= '是'  then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_s,
                            sum(case when wdwhjgfjgqsyyrkrgbdj= '否' OR wdwhjgfjgqsyyrkrgbdj is null  then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_f,
                            sum(case when sfczckfhwggptzl= '是'  then 1 else 0 end)  sfczckfhwggptzl_s,
                            sum(case when sfczckfhwggptzl= '否' OR sfczckfhwggptzl is null then 1 else 0 end)  sfczckfhwggptzl_f,
                            sum(case when use_electric_bicycle= '是' then 1 else 0 end) use_electric_bicycle_s,
                            sum(case when use_electric_bicycle= '否' OR use_electric_bicycle is null then 1 else 0 end)  use_electric_bicycle_f,
                            sum(case when fwyfwdddzxccdsssfayqjshsf= '是'  then 1 else 0 end) fwyfwdddzxccdsssfayqjshsf_s,
                            sum(case when fwyfwdddzxccdsssfayqjshsf= '否' OR fwyfwdddzxccdsssfayqjshsf is null  then 1 else 0 end)  fwyfwdddzxccdsssfayqjshsf_f,
                            sum(case when use_bottled_liquefied_gas= '是' then 1 else 0 end) use_bottled_liquefied_gas_s,
                            sum(case when use_bottled_liquefied_gas= '否' OR use_bottled_liquefied_gas is null then 1 else 0 end)  use_bottled_liquefied_gas_f,
                            sum(case when use_stacked_items= '是' then 1 else 0 end) use_stacked_items_s,
                            sum(case when use_stacked_items= '否' OR use_stacked_items is null then 1 else 0 end)  use_stacked_items_f,
                            sum(case when slljdx= '是' then 1 else 0 end) slljdx_s,
                            sum(case when slljdx= '否' OR slljdx is null then 1 else 0 end)  slljdx_f,
                            sum(case when notes is not null then 1 else 0 end) notes_s,
                            sum(case when notes is null then 1 else 0 end)  notes_f
                             from(select * from(select ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num, t1.xzq, t1.xzqdm, t1.towndm, t1.town, t1.villagedm, t1.village, t1.hzxm, t1.address, t1.fwbh, t1.cs, t1.jsrysstd, t1.create_on, t1.create_name, t1.yhly,case when t1.wdwhjgfjgqsyyrkrgbdj = 1 then '是' when t1.wdwhjgfjgqsyyrkrgbdj = '0' then '否' end wdwhjgfjgqsyyrkrgbdj,
                            case when t1.sstdsyyrkrclzx = 1 then '是' when t1.sstdsyyrkrclzx = '0' then '否' end sstdsyyrkrclzx,
                            case when t1.gryssdcsszblyyqksddp = 1 then '是' when t1.gryssdcsszblyyqksddp = '0' then '否' end gryssdcsszblyyqksddp,
                            case when t1.fwyfwdddzxccdsssfayqjshsf = 1 then '是' when t1.fwyfwdddzxccdsssfayqjshsf = '0' then '否' end fwyfwdddzxccdsssfayqjshsf,
                            case when use_electric_bicycle= 1 then '是' when use_electric_bicycle = '0' then '否' end use_electric_bicycle,
                            case when use_bottled_liquefied_gas= 1 then '是' when use_bottled_liquefied_gas = '0' then '否' end use_bottled_liquefied_gas,
                            case when use_stacked_items= 1 then '是' when use_stacked_items = '0' then '否' end use_stacked_items,
                            case when mhqwbcwhyx= 1 then '是' when mhqwbcwhyx = '0' then '否' end mhqwbcwhyx,
                            case when jyplmhxtwbcwhyx= '1' then '是' when jyplmhxtwbcwhyx = '0' then '否' end jyplmhxtwbcwhyx,
                            case when ygbjwfwhyx= '1' then '是' when ygbjwfwhyx = '0' then '否' end ygbjwfwhyx,
                            case when slljdx= '1' then '是' when slljdx = '0' then '否' end slljdx,
                            case when sfczckfhwggptzl= '1' then '是' when sfczckfhwggptzl = '0' then '否' end sfczckfhwggptzl, notes from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id = t2.base_id and t1.yhly='" + yhly + "' and t1.create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59' ) t " +
                         " where row_num = 1 and yhly = '" + yhly + "' and create_on::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP   and fwbh not in(select distinct fwbh from "+table_fwzt+"  where final_fwzt is not null and   final_create_on='"+endTime+" 23:59:59')  " +
                         "   order by create_on desc, xzqdm, village asc ) t group by xzq, xzqdm order by xzqdm) t right join (select DISTINCT xzq,xzqdm from zm_building_ledger) k on t.xzqdm=k.xzqdm  where  1=1 ) t  where  1=1 " + strWhere + "";
            if (sql_count.Contains("insert into", StringComparison.OrdinalIgnoreCase)
                || sql_count.Contains("delete from", StringComparison.OrdinalIgnoreCase)
                || sql_count.Contains("update ", StringComparison.OrdinalIgnoreCase))
            {
                return new RespRet<Page<object>>(false, "不符合规范，禁止执行");
            }

            int count = _dbContext.Database.GetDbConnection().QueryFirstOrDefault<int>(sql_count);
            var datas = _dbContext.Database.GetDbConnection().Query<object>(sql).ToList();
            return new RespRet<Page<object>>(true, "ok", new Page<object> { Items = datas, NextPageIndex = pageindex + 1, PageTotal = count, Total = count });
        }

        /// <summary>
        /// 统计核验变化情况（区级）
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpGet("yhtj/yhtjtownlist")]
        [Produces("application/json")]
        public RespRet<Page<object>> yhtjtownlistbydate([FromQuery] string xzqdm, [FromQuery] string towndm, [FromQuery] string villagedm, [FromQuery] int pageindex, [FromQuery] int pagesize, [FromQuery] string startTime, [FromQuery] string endTime, [FromQuery] string txtKeyWord, [FromQuery] string yhly)
        {
            if (pageindex < 0)
            {
                return new RespRet<Page<object>>(false, "PageIndex must >= 0");
            }

            var userNumber = this.CurrentUser();
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<Page<object>>(false, "user not logined");
            }
            var user = _userRepository.FirstOrDefault(u => u.Number == userNumber && u.IsDelete != 1);
            if (null == user)
            {
                return new RespRet<Page<object>>(false, "用户名不存在");
            }
            string district = user.District;

            var options = new JsonSerializerOptions();
            options.PropertyNameCaseInsensitive = true; // 设置属性不区分大小写
            Xzq model = System.Text.Json.JsonSerializer.Deserialize<Xzq>(district, options);

            string toDay = DateTime.Now.ToString("yyyy-MM-dd");
            string table_fwzt = "";
            if (!string.IsNullOrEmpty(endTime))
            {
                DateTime endData = Convert.ToDateTime(endTime);
                table_fwzt = "zm_building_fwzt_" + endData.Year;
                if (Convert.ToDateTime(endTime) > DateTime.Now)
                {
                    table_fwzt = "zm_building_fwzt_" + DateTime.Now.Year;
                }
            }
            else
            {
                DateTime endData = Convert.ToDateTime(toDay);
                table_fwzt = "zm_building_fwzt_" + endData.Year;
            }



            string strWhere = "";
            if (!string.IsNullOrEmpty(xzqdm))
            {
                if (user.RoleType == 1 || user.RoleType == 2)
                {
                    strWhere += "and xzqdm ='" + xzqdm + "'";
                }
                else if (user.RoleType == 3)
                {
                    strWhere += " and xzqdm='" + model.RegionCode + "'";
                }
                else
                {
                    towndm = model.RegionCode;
                    if (model.RegionCode.Length != 12)
                    {
                        towndm = model.RegionCode + "000";
                    }
                    strWhere += " and town='" + towndm + "'";
                }
            }

            if (string.IsNullOrEmpty(startTime) && string.IsNullOrEmpty(endTime))
            {
                startTime = DateTime.Now.ToShortDateString();
                endTime = DateTime.Now.ToShortDateString();
            }
            if (!string.IsNullOrEmpty(txtKeyWord))
            {
                strWhere += " and (fwbh='" + txtKeyWord + "' or hzxm like '%" + txtKeyWord + "%' OR address like '%" + txtKeyWord + "%')";
            }
            if (!string.IsNullOrEmpty(xzqdm) && !string.IsNullOrEmpty(towndm) && !string.IsNullOrEmpty(villagedm))
            {
                strWhere += " and xzqdm='" + xzqdm + "' and town='" + towndm + "' and village='" + villagedm + "'";
            }
            else if (!string.IsNullOrEmpty(xzqdm) && !string.IsNullOrEmpty(towndm) && string.IsNullOrEmpty(villagedm))
            {
                strWhere += " and xzqdm='" + xzqdm + "' and town='" + towndm + "' ";
            }
            else if (!string.IsNullOrEmpty(xzqdm) && string.IsNullOrEmpty(towndm) && string.IsNullOrEmpty(villagedm))
            {
                strWhere += " and xzqdm='" + xzqdm + "' ";
            }
            if (string.IsNullOrEmpty(yhly))
            {
                yhly = "0";
            }

            string sql = @$"select  ROW_NUMBER() OVER ()index,*  from (select t.*,
 COALESCE(tt.use_electric_bicycle_wzg, 0) use_electric_bicycle_wzg,
 COALESCE(tt.use_electric_bicycle_yzg, 0) use_electric_bicycle_yzg,
 COALESCE(tt.use_bottled_liquefied_gas_wzg, 0) use_bottled_liquefied_gas_wzg,
 COALESCE(tt.use_bottled_liquefied_gas_yzg, 0) use_bottled_liquefied_gas_yzg,
 COALESCE(tt.use_stacked_items_wzg, 0) use_stacked_items_wzg,
 COALESCE(tt.use_stacked_items_yzg, 0) use_stacked_items_yzg,
 COALESCE(tt.mhqwbcwhyx_wzg, 0) mhqwbcwhyx_wzg,
 COALESCE(tt.mhqwbcwhyx_yzg, 0) mhqwbcwhyx_yzg,
 COALESCE(tt.jyplmhxtwbcwhyx_wzg, 0) jyplmhxtwbcwhyx_wzg,
 COALESCE(tt.jyplmhxtwbcwhyx_yzg, 0) jyplmhxtwbcwhyx_yzg,
 COALESCE(tt.ygbjwfwhyx_wzg, 0) ygbjwfwhyx_wzg,
 COALESCE(tt.ygbjwfwhyx_yzg, 0) ygbjwfwhyx_yzg,
 COALESCE(tt.slljdx_wzg, 0) slljdx_wzg,
 COALESCE(tt.slljdx_yzg, 0) slljdx_yzg,
 COALESCE(tt.sfczckfhwggptzl_wzg, 0) sfczckfhwggptzl_wzg,
 COALESCE(tt.sfczckfhwggptzl_yzg, 0) sfczckfhwggptzl_yzg 
 from  (select  k.xzq,k.xzqdm,k.town,k.towndm ,COALESCE(cnt, 0) AS cnt,COALESCE(jsrysstd_s, 0) AS jsrysstd_s,COALESCE(jsrysstd_f, 0) AS jsrysstd_f,COALESCE(wdwhjgfjgqsyyrkrgbdj_s, 0) AS wdwhjgfjgqsyyrkrgbdj_s,COALESCE(wdwhjgfjgqsyyrkrgbdj_f, 0) AS wdwhjgfjgqsyyrkrgbdj_f,COALESCE(sfczckfhwggptzl_s, 0) AS sfczckfhwggptzl_s,	COALESCE(sfczckfhwggptzl_f, 0) AS sfczckfhwggptzl_f,COALESCE(use_electric_bicycle_s, 0) AS use_electric_bicycle_s,COALESCE(use_electric_bicycle_f, 0) AS use_electric_bicycle_f,COALESCE(fwyfwdddzxccdsssfayqjshsf_s, 0) AS fwyfwdddzxccdsssfayqjshsf_s,COALESCE(fwyfwdddzxccdsssfayqjshsf_f, 0) AS fwyfwdddzxccdsssfayqjshsf_f,COALESCE(use_bottled_liquefied_gas_s, 0) AS use_bottled_liquefied_gas_s,COALESCE(use_bottled_liquefied_gas_f, 0) AS use_bottled_liquefied_gas_f,COALESCE(use_stacked_items_s, 0) AS use_stacked_items_s,COALESCE(use_stacked_items_f, 0) AS use_stacked_items_f,
							COALESCE(slljdx_s, 0) AS slljdx_s,COALESCE(slljdx_f, 0) AS slljdx_f,COALESCE(notes_s, 0) AS notes_s,COALESCE(notes_f, 0) AS notes_f from (select t.*,m.town
                            
                             from(select xzqdm,xzq,town towndm,count(1) cnt,sum(case when jsrysstd= '是'   then 1 else 0 end)  jsrysstd_s,
                            sum(case when jsrysstd= '否' OR jsrysstd is null then 1 else 0 end)  jsrysstd_f,
                            sum(case when wdwhjgfjgqsyyrkrgbdj= '是'  then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_s,
                            sum(case when wdwhjgfjgqsyyrkrgbdj= '否' OR wdwhjgfjgqsyyrkrgbdj is null  then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_f,
                            sum(case when sfczckfhwggptzl= '是'  then 1 else 0 end)  sfczckfhwggptzl_s,
                            sum(case when sfczckfhwggptzl= '否' OR sfczckfhwggptzl is null  then 1 else 0 end)  sfczckfhwggptzl_f,
                            sum(case when use_electric_bicycle= '是'  then 1 else 0 end) use_electric_bicycle_s,
                            sum(case when use_electric_bicycle= '否' OR use_electric_bicycle is null  then 1 else 0 end)  use_electric_bicycle_f,
                            sum(case when fwyfwdddzxccdsssfayqjshsf= '是'  then 1 else 0 end) fwyfwdddzxccdsssfayqjshsf_s,
                            sum(case when fwyfwdddzxccdsssfayqjshsf= '否'  OR fwyfwdddzxccdsssfayqjshsf is null then 1 else 0 end)  fwyfwdddzxccdsssfayqjshsf_f,
                            sum(case when use_bottled_liquefied_gas= '是'   then 1 else 0 end) use_bottled_liquefied_gas_s,
                            sum(case when use_bottled_liquefied_gas= '否' OR use_bottled_liquefied_gas is null then 1 else 0 end)  use_bottled_liquefied_gas_f,
                            sum(case when use_stacked_items= '是' OR use_stacked_items is null then 1 else 0 end) use_stacked_items_s,
                            sum(case when use_stacked_items= '否'  then 1 else 0 end)  use_stacked_items_f,
                            sum(case when slljdx= '是' OR slljdx is null then 1 else 0 end) slljdx_s,
                            sum(case when slljdx= '否'  then 1 else 0 end)  slljdx_f,
                            sum(case when notes is not null then 1 else 0 end) notes_s,
                            sum(case when notes is null then 1 else 0 end)  notes_f from(select ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num, t1.xzq, t1.xzqdm, t1.towndm, t1.town, t1.villagedm, t1.village, t1.hzxm, t1.address, t1.fwbh, t1.cs, t1.jsrysstd, t1.create_on, t1.create_name, t1.yhly,case when t1.wdwhjgfjgqsyyrkrgbdj = 1 then '是' when t1.wdwhjgfjgqsyyrkrgbdj = '0' then '否' end wdwhjgfjgqsyyrkrgbdj,
                            case when t1.sstdsyyrkrclzx = 1 then '是' when t1.sstdsyyrkrclzx = '0' then '否' end sstdsyyrkrclzx,
                            case when t1.gryssdcsszblyyqksddp = 1 then '是' when t1.gryssdcsszblyyqksddp = '0' then '否' end gryssdcsszblyyqksddp,
                            case when t1.fwyfwdddzxccdsssfayqjshsf = 1 then '是' when t1.fwyfwdddzxccdsssfayqjshsf = '0' then '否' end fwyfwdddzxccdsssfayqjshsf,
                            case when use_electric_bicycle= 1 then '是' when use_electric_bicycle = '0' then '否' end use_electric_bicycle,
                            case when use_bottled_liquefied_gas= 1 then '是' when use_bottled_liquefied_gas = '0' then '否' end use_bottled_liquefied_gas,
                            case when use_stacked_items= 1 then '是' when use_stacked_items = '0' then '否' end use_stacked_items,
                            case when mhqwbcwhyx= 1 then '是' when mhqwbcwhyx = '0' then '否' end mhqwbcwhyx,
                            case when jyplmhxtwbcwhyx= '1' then '是' when jyplmhxtwbcwhyx = '0' then '否' end jyplmhxtwbcwhyx,
                            case when ygbjwfwhyx= '1' then '是' when ygbjwfwhyx = '0' then '否' end ygbjwfwhyx,
                            case when slljdx= '1' then '是' when slljdx = '0' then '否' end slljdx,
                            case when sfczckfhwggptzl= '1' then '是' when sfczckfhwggptzl = '0' then '否' end sfczckfhwggptzl, notes from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id = t2.base_id and t1.yhly='" + yhly + "' and t1.create_on  BETWEEN '" + startTime + "'  and '" + endTime + " 23:59:59') t " +
                           " where row_num = 1 and yhly = '" + yhly + "' and create_on  BETWEEN '" + startTime + "'  and '" + endTime + " 23:59:59'  and fwbh not in(select fwbh From "+table_fwzt+ " where final_create_on='" + endTime + " 23:59:59' and final_fwzt is not null )  " +
                           "  group by xzq,xzqdm,town ) t , (select distinct towndm ,town from zm_building_ledger) m where t.towndm=m.towndm ) t right join (select DISTINCT xzq,xzqdm,town,towndm from zm_building_ledger where xzqdm='" + xzqdm + "' ) k on t.towndm=k.towndm  where  1=1 ) t " +
                           "left join (select  a.towndm,count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='use_electric_bicycle' then 1 end) use_electric_bicycle_wzg,count(case when fh_zt=0 and a.check_id='use_electric_bicycle' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)use_electric_bicycle_yzg ," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='use_bottled_liquefied_gas' then 1 end) use_bottled_liquefied_gas_wzg," +
                           "count(case when fh_zt=0 and a.check_id='use_bottled_liquefied_gas' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)use_bottled_liquefied_gas_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='use_stacked_items' then 1 end) use_stacked_items_wzg," +
                           "count(case when fh_zt=0 and a.check_id='use_stacked_items' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)use_stacked_items_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='mhqwbcwhyx' then 1 end) mhqwbcwhyx_wzg," +
                           "count(case when fh_zt=0 and a.check_id='mhqwbcwhyx' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP   then 1 end)mhqwbcwhyx_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='jyplmhxtwbcwhyx' then 1 end)jyplmhxtwbcwhyx_wzg," +
                           "count(case when fh_zt=0 and a.check_id='jyplmhxtwbcwhyx' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)jyplmhxtwbcwhyx_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='ygbjwfwhyx' then 1 end)ygbjwfwhyx_wzg," +
                           "count(case when fh_zt=0 and a.check_id='ygbjwfwhyx' and fh_time_dx::TIMESTAMP  BETWEEN  '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)ygbjwfwhyx_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='slljdx' then 1 end)slljdx_wzg," +
                           "count(case when fh_zt=0 and a.check_id='slljdx' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)slljdx_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='sfczckfhwggptzl' then 1 end)sfczckfhwggptzl_wzg," +
                           "count(case when fh_zt=0 and a.check_id='sfczckfhwggptzl' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)sfczckfhwggptzl_yzg from zm_pc_yhlog_dx_sqjz a  where a.jcly='" + yhly + "' and a.create_on::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP   GROUP BY a.towndm)tt on t.towndm=tt.towndm)t where  1=1 " + strWhere;

            if (sql.Contains("insert into", StringComparison.OrdinalIgnoreCase)
                || sql.Contains("delete from", StringComparison.OrdinalIgnoreCase)
                || sql.Contains("update ", StringComparison.OrdinalIgnoreCase))
            {
                return new RespRet<Page<object>>(false, "不符合规范，禁止执行");
            }
            string sql_count = @$"select count(1) cnt  from (select t.*,
 COALESCE(tt.use_electric_bicycle_wzg, 0) use_electric_bicycle_wzg,
 COALESCE(tt.use_electric_bicycle_yzg, 0) use_electric_bicycle_yzg,
 COALESCE(tt.use_bottled_liquefied_gas_wzg, 0) use_bottled_liquefied_gas_wzg,
 COALESCE(tt.use_bottled_liquefied_gas_yzg, 0) use_bottled_liquefied_gas_yzg,
 COALESCE(tt.use_stacked_items_wzg, 0) use_stacked_items_wzg,
 COALESCE(tt.use_stacked_items_yzg, 0) use_stacked_items_yzg,
 COALESCE(tt.mhqwbcwhyx_wzg, 0) mhqwbcwhyx_wzg,
 COALESCE(tt.mhqwbcwhyx_yzg, 0) mhqwbcwhyx_yzg,
 COALESCE(tt.jyplmhxtwbcwhyx_wzg, 0) jyplmhxtwbcwhyx_wzg,
 COALESCE(tt.jyplmhxtwbcwhyx_yzg, 0) jyplmhxtwbcwhyx_yzg,
 COALESCE(tt.ygbjwfwhyx_wzg, 0) ygbjwfwhyx_wzg,
 COALESCE(tt.ygbjwfwhyx_yzg, 0) ygbjwfwhyx_yzg,
 COALESCE(tt.slljdx_wzg, 0) slljdx_wzg,
 COALESCE(tt.slljdx_yzg, 0) slljdx_yzg,
 COALESCE(tt.sfczckfhwggptzl_wzg, 0) sfczckfhwggptzl_wzg,
 COALESCE(tt.sfczckfhwggptzl_yzg, 0) sfczckfhwggptzl_yzg 
 from  (select  k.xzq,k.xzqdm,k.town,k.towndm ,COALESCE(cnt, 0) AS cnt,COALESCE(jsrysstd_s, 0) AS jsrysstd_s,COALESCE(jsrysstd_f, 0) AS jsrysstd_f,COALESCE(wdwhjgfjgqsyyrkrgbdj_s, 0) AS wdwhjgfjgqsyyrkrgbdj_s,COALESCE(wdwhjgfjgqsyyrkrgbdj_f, 0) AS wdwhjgfjgqsyyrkrgbdj_f,COALESCE(sfczckfhwggptzl_s, 0) AS sfczckfhwggptzl_s,	COALESCE(sfczckfhwggptzl_f, 0) AS sfczckfhwggptzl_f,COALESCE(use_electric_bicycle_s, 0) AS use_electric_bicycle_s,COALESCE(use_electric_bicycle_f, 0) AS use_electric_bicycle_f,COALESCE(fwyfwdddzxccdsssfayqjshsf_s, 0) AS fwyfwdddzxccdsssfayqjshsf_s,COALESCE(fwyfwdddzxccdsssfayqjshsf_f, 0) AS fwyfwdddzxccdsssfayqjshsf_f,COALESCE(use_bottled_liquefied_gas_s, 0) AS use_bottled_liquefied_gas_s,COALESCE(use_bottled_liquefied_gas_f, 0) AS use_bottled_liquefied_gas_f,COALESCE(use_stacked_items_s, 0) AS use_stacked_items_s,COALESCE(use_stacked_items_f, 0) AS use_stacked_items_f,
							COALESCE(slljdx_s, 0) AS slljdx_s,COALESCE(slljdx_f, 0) AS slljdx_f,COALESCE(notes_s, 0) AS notes_s,COALESCE(notes_f, 0) AS notes_f from (select t.*,m.town
                            
                             from(select xzqdm,xzq,town towndm,count(1) cnt,sum(case when jsrysstd= '是'   then 1 else 0 end)  jsrysstd_s,
                            sum(case when jsrysstd= '否' OR jsrysstd is null then 1 else 0 end)  jsrysstd_f,
                            sum(case when wdwhjgfjgqsyyrkrgbdj= '是'  then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_s,
                            sum(case when wdwhjgfjgqsyyrkrgbdj= '否' OR wdwhjgfjgqsyyrkrgbdj is null  then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_f,
                            sum(case when sfczckfhwggptzl= '是'  then 1 else 0 end)  sfczckfhwggptzl_s,
                            sum(case when sfczckfhwggptzl= '否' OR sfczckfhwggptzl is null  then 1 else 0 end)  sfczckfhwggptzl_f,
                            sum(case when use_electric_bicycle= '是'  then 1 else 0 end) use_electric_bicycle_s,
                            sum(case when use_electric_bicycle= '否' OR use_electric_bicycle is null  then 1 else 0 end)  use_electric_bicycle_f,
                            sum(case when fwyfwdddzxccdsssfayqjshsf= '是'  then 1 else 0 end) fwyfwdddzxccdsssfayqjshsf_s,
                            sum(case when fwyfwdddzxccdsssfayqjshsf= '否'  OR fwyfwdddzxccdsssfayqjshsf is null then 1 else 0 end)  fwyfwdddzxccdsssfayqjshsf_f,
                            sum(case when use_bottled_liquefied_gas= '是'   then 1 else 0 end) use_bottled_liquefied_gas_s,
                            sum(case when use_bottled_liquefied_gas= '否' OR use_bottled_liquefied_gas is null then 1 else 0 end)  use_bottled_liquefied_gas_f,
                            sum(case when use_stacked_items= '是' OR use_stacked_items is null then 1 else 0 end) use_stacked_items_s,
                            sum(case when use_stacked_items= '否'  then 1 else 0 end)  use_stacked_items_f,
                            sum(case when slljdx= '是' OR slljdx is null then 1 else 0 end) slljdx_s,
                            sum(case when slljdx= '否'  then 1 else 0 end)  slljdx_f,
                            sum(case when notes is not null then 1 else 0 end) notes_s,
                            sum(case when notes is null then 1 else 0 end)  notes_f from(select ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num, t1.xzq, t1.xzqdm, t1.towndm, t1.town, t1.villagedm, t1.village, t1.hzxm, t1.address, t1.fwbh, t1.cs, t1.jsrysstd, t1.create_on, t1.create_name, t1.yhly,case when t1.wdwhjgfjgqsyyrkrgbdj = 1 then '是' when t1.wdwhjgfjgqsyyrkrgbdj = '0' then '否' end wdwhjgfjgqsyyrkrgbdj,
                            case when t1.sstdsyyrkrclzx = 1 then '是' when t1.sstdsyyrkrclzx = '0' then '否' end sstdsyyrkrclzx,
                            case when t1.gryssdcsszblyyqksddp = 1 then '是' when t1.gryssdcsszblyyqksddp = '0' then '否' end gryssdcsszblyyqksddp,
                            case when t1.fwyfwdddzxccdsssfayqjshsf = 1 then '是' when t1.fwyfwdddzxccdsssfayqjshsf = '0' then '否' end fwyfwdddzxccdsssfayqjshsf,
                            case when use_electric_bicycle= 1 then '是' when use_electric_bicycle = '0' then '否' end use_electric_bicycle,
                            case when use_bottled_liquefied_gas= 1 then '是' when use_bottled_liquefied_gas = '0' then '否' end use_bottled_liquefied_gas,
                            case when use_stacked_items= 1 then '是' when use_stacked_items = '0' then '否' end use_stacked_items,
                            case when mhqwbcwhyx= 1 then '是' when mhqwbcwhyx = '0' then '否' end mhqwbcwhyx,
                            case when jyplmhxtwbcwhyx= '1' then '是' when jyplmhxtwbcwhyx = '0' then '否' end jyplmhxtwbcwhyx,
                            case when ygbjwfwhyx= '1' then '是' when ygbjwfwhyx = '0' then '否' end ygbjwfwhyx,
                            case when slljdx= '1' then '是' when slljdx = '0' then '否' end slljdx,
                            case when sfczckfhwggptzl= '1' then '是' when sfczckfhwggptzl = '0' then '否' end sfczckfhwggptzl, notes from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id = t2.base_id and t1.yhly='" + yhly + "' and t1.create_on  BETWEEN '" + startTime + "'  and '" + endTime + " 23:59:59') t " +
                           " where row_num = 1 and yhly = '" + yhly + "' and create_on  BETWEEN '" + startTime + "'  and '" + endTime + " 23:59:59'   and fwbh not in(select fwbh From "+table_fwzt+ " where final_create_on='" + endTime + " 23:59:59' and final_fwzt is not null )  " +
                           "   group by xzq,xzqdm,town ) t , (select distinct towndm ,town from zm_building_ledger) m where t.towndm=m.towndm ) t right join (select DISTINCT xzq,xzqdm,town,towndm from zm_building_ledger where xzqdm='" + xzqdm + "' ) k on t.towndm=k.towndm  where  1=1 ) t " +
                           "left join (select  a.towndm,count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='use_electric_bicycle' then 1 end) use_electric_bicycle_wzg,count(case when fh_zt=0 and a.check_id='use_electric_bicycle' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)use_electric_bicycle_yzg ," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='use_bottled_liquefied_gas' then 1 end) use_bottled_liquefied_gas_wzg," +
                           "count(case when fh_zt=0 and a.check_id='use_bottled_liquefied_gas' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)use_bottled_liquefied_gas_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='use_stacked_items' then 1 end) use_stacked_items_wzg," +
                           "count(case when fh_zt=0 and a.check_id='use_stacked_items' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)use_stacked_items_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='mhqwbcwhyx' then 1 end) mhqwbcwhyx_wzg," +
                           "count(case when fh_zt=0 and a.check_id='mhqwbcwhyx' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP   then 1 end)mhqwbcwhyx_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='jyplmhxtwbcwhyx' then 1 end)jyplmhxtwbcwhyx_wzg," +
                           "count(case when fh_zt=0 and a.check_id='jyplmhxtwbcwhyx' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)jyplmhxtwbcwhyx_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='ygbjwfwhyx' then 1 end)ygbjwfwhyx_wzg," +
                           "count(case when fh_zt=0 and a.check_id='ygbjwfwhyx' and fh_time_dx::TIMESTAMP  BETWEEN  '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)ygbjwfwhyx_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='slljdx' then 1 end)slljdx_wzg," +
                           "count(case when fh_zt=0 and a.check_id='slljdx' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)slljdx_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='sfczckfhwggptzl' then 1 end)sfczckfhwggptzl_wzg," +
                           "count(case when fh_zt=0 and a.check_id='sfczckfhwggptzl' and fh_time_dx::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)sfczckfhwggptzl_yzg from zm_pc_yhlog_dx_sqjz a  where a.jcly='" + yhly + "' and a.create_on::TIMESTAMP   BETWEEN '" + startTime + "'::TIMESTAMP  and '" + endTime + " 23:59:59'::TIMESTAMP   GROUP BY a.towndm)tt on t.towndm=tt.towndm)t where  1=1 " + strWhere;

            if (sql_count.Contains("insert into", StringComparison.OrdinalIgnoreCase)
                || sql_count.Contains("delete from", StringComparison.OrdinalIgnoreCase)
                || sql_count.Contains("update ", StringComparison.OrdinalIgnoreCase))
            {
                return new RespRet<Page<object>>(false, "不符合规范，禁止执行");
            }


            int count = _dbContext.Database.GetDbConnection().QueryFirstOrDefault<int>(sql_count);
            var datas = _dbContext.Database.GetDbConnection().Query<object>(sql).ToList();
            return new RespRet<Page<object>>(true, "ok", new Page<object> { Items = datas, NextPageIndex = pageindex + 1, PageTotal = count, Total = count });
        }


        /// <summary>
        /// 统计核验变化情况（区级）
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpGet("yhtj/yhtjvillagelist")]
        [Produces("application/json")]
        public RespRet<Page<object>> yhtjvillagelistbydate([FromQuery] string xzqdm, [FromQuery] string towndm, [FromQuery] string villagedm, [FromQuery] int pageindex, [FromQuery] int pagesize, [FromQuery] string startTime, [FromQuery] string endTime, [FromQuery] string txtKeyWord, [FromQuery] string yhly)
        {
            if (pageindex < 0)
            {
                return new RespRet<Page<object>>(false, "PageIndex must >= 0");
            }

            var userNumber = this.CurrentUser();
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<Page<object>>(false, "user not logined");
            }
            var user = _userRepository.FirstOrDefault(u => u.Number == userNumber && u.IsDelete != 1);
            if (null == user)
            {
                return new RespRet<Page<object>>(false, "用户名不存在");
            }
            string district = user.District;

            var options = new JsonSerializerOptions();
            options.PropertyNameCaseInsensitive = true; // 设置属性不区分大小写
            Xzq model = System.Text.Json.JsonSerializer.Deserialize<Xzq>(district, options);

            string toDay = DateTime.Now.ToString("yyyy-MM-dd");
            string table_fwzt = "";
            if (!string.IsNullOrEmpty(endTime))
            {
                DateTime endData = Convert.ToDateTime(endTime);
                table_fwzt = "zm_building_fwzt_" + endData.Year;
                if (Convert.ToDateTime(endTime) > DateTime.Now)
                {
                    table_fwzt = "zm_building_fwzt_" + DateTime.Now.Year;
                }
            }
            else
            {
                DateTime endData = Convert.ToDateTime(toDay);
                table_fwzt = "zm_building_fwzt_" + endData.Year;
            }



            string strWhere = "";
            if (!string.IsNullOrEmpty(xzqdm))
            {
                if (user.RoleType == 1 || user.RoleType == 2)
                {
                    strWhere += "and xzqdm ='" + xzqdm + "'";
                }
                else if (user.RoleType == 3)
                {
                    strWhere += " and xzqdm='" + model.RegionCode + "'";
                }
                else
                {
                    towndm = model.RegionCode;
                    if (model.RegionCode.Length != 12)
                    {
                        towndm = model.RegionCode + "000";
                    }
                    strWhere += " and town='" + towndm + "'";
                }
            }

            if (string.IsNullOrEmpty(startTime) && string.IsNullOrEmpty(endTime))
            {
                startTime = DateTime.Now.ToShortDateString();
                endTime = DateTime.Now.ToShortDateString();
            }
            if (!string.IsNullOrEmpty(txtKeyWord))
            {
                strWhere += " and (fwbh='" + txtKeyWord + "' or hzxm like '%" + txtKeyWord + "%' OR address like '%" + txtKeyWord + "%')";
            }
            if (!string.IsNullOrEmpty(xzqdm) && !string.IsNullOrEmpty(towndm) && !string.IsNullOrEmpty(villagedm))
            {
                strWhere += " and xzqdm='" + xzqdm + "' and towndm='" + towndm + "' and villagedm='" + villagedm + "'";
            }
            else if (!string.IsNullOrEmpty(xzqdm) && !string.IsNullOrEmpty(towndm) && string.IsNullOrEmpty(villagedm))
            {
                strWhere += " and xzqdm='" + xzqdm + "' and towndm='" + towndm + "' ";
            }
            else if (!string.IsNullOrEmpty(xzqdm) && string.IsNullOrEmpty(towndm) && string.IsNullOrEmpty(villagedm))
            {
                strWhere += " and xzqdm='" + xzqdm + "' ";
            }
            if (string.IsNullOrEmpty(yhly))
            {
                yhly = "0";
            }

            string sql = @"select  ROW_NUMBER() OVER ()index,*  from (select t.*,
 COALESCE(tt.use_electric_bicycle_wzg, 0) use_electric_bicycle_wzg,
 COALESCE(tt.use_electric_bicycle_yzg, 0) use_electric_bicycle_yzg,
 COALESCE(tt.use_bottled_liquefied_gas_wzg, 0) use_bottled_liquefied_gas_wzg,
 COALESCE(tt.use_bottled_liquefied_gas_yzg, 0) use_bottled_liquefied_gas_yzg,
 COALESCE(tt.use_stacked_items_wzg, 0) use_stacked_items_wzg,
 COALESCE(tt.use_stacked_items_yzg, 0) use_stacked_items_yzg,
 COALESCE(tt.mhqwbcwhyx_wzg, 0) mhqwbcwhyx_wzg,
 COALESCE(tt.mhqwbcwhyx_yzg, 0) mhqwbcwhyx_yzg,
 COALESCE(tt.jyplmhxtwbcwhyx_wzg, 0) jyplmhxtwbcwhyx_wzg,
 COALESCE(tt.jyplmhxtwbcwhyx_yzg, 0) jyplmhxtwbcwhyx_yzg,
 COALESCE(tt.ygbjwfwhyx_wzg, 0) ygbjwfwhyx_wzg,
 COALESCE(tt.ygbjwfwhyx_yzg, 0) ygbjwfwhyx_yzg,
 COALESCE(tt.slljdx_wzg, 0) slljdx_wzg,
 COALESCE(tt.slljdx_yzg, 0) slljdx_yzg,
 COALESCE(tt.sfczckfhwggptzl_wzg, 0) sfczckfhwggptzl_wzg,
 COALESCE(tt.sfczckfhwggptzl_yzg, 0) sfczckfhwggptzl_yzg 
 from (select  	k.xzq,k.xzqdm,k.town,k.towndm,k.village,k.villagedm, COALESCE(cnt, 0) AS cnt,COALESCE(jsrysstd_s, 0) AS jsrysstd_s,COALESCE(jsrysstd_f, 0) AS jsrysstd_f,COALESCE(wdwhjgfjgqsyyrkrgbdj_s, 0) AS wdwhjgfjgqsyyrkrgbdj_s,COALESCE(wdwhjgfjgqsyyrkrgbdj_f, 0) AS wdwhjgfjgqsyyrkrgbdj_f,COALESCE(sfczckfhwggptzl_s, 0) AS sfczckfhwggptzl_s,	COALESCE(sfczckfhwggptzl_f, 0) AS sfczckfhwggptzl_f,COALESCE(use_electric_bicycle_s, 0) AS use_electric_bicycle_s,COALESCE(use_electric_bicycle_f, 0) AS use_electric_bicycle_f,COALESCE(fwyfwdddzxccdsssfayqjshsf_s, 0) AS fwyfwdddzxccdsssfayqjshsf_s,COALESCE(fwyfwdddzxccdsssfayqjshsf_f, 0) AS fwyfwdddzxccdsssfayqjshsf_f,COALESCE(use_bottled_liquefied_gas_s, 0) AS use_bottled_liquefied_gas_s,COALESCE(use_bottled_liquefied_gas_f, 0) AS use_bottled_liquefied_gas_f,COALESCE(use_stacked_items_s, 0) AS use_stacked_items_s,COALESCE(use_stacked_items_f, 0) AS use_stacked_items_f,
							COALESCE(slljdx_s, 0) AS slljdx_s,COALESCE(slljdx_f, 0) AS slljdx_f,COALESCE(notes_s, 0) AS notes_s,COALESCE(notes_f, 0) AS notes_f from (select t.*,k.town ,k.village
                             from(select xzqdm,xzq,town towndm,village villagedm,count(1)cnt,
                            sum(case when jsrysstd= '是'   then 1 else 0 end)  jsrysstd_s,
                            sum(case when jsrysstd= '否' OR jsrysstd is null then 1 else 0 end)  jsrysstd_f,
                            sum(case when wdwhjgfjgqsyyrkrgbdj= '是'  then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_s,
                            sum(case when wdwhjgfjgqsyyrkrgbdj= '否' OR wdwhjgfjgqsyyrkrgbdj is null  then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_f,
                            sum(case when sfczckfhwggptzl= '是'  then 1 else 0 end)  sfczckfhwggptzl_s,
                            sum(case when sfczckfhwggptzl= '否' OR sfczckfhwggptzl is null  then 1 else 0 end)  sfczckfhwggptzl_f,
                            sum(case when use_electric_bicycle= '是'  then 1 else 0 end) use_electric_bicycle_s,
                            sum(case when use_electric_bicycle= '否' OR use_electric_bicycle is null  then 1 else 0 end)  use_electric_bicycle_f,
                            sum(case when fwyfwdddzxccdsssfayqjshsf= '是'  then 1 else 0 end) fwyfwdddzxccdsssfayqjshsf_s,
                            sum(case when fwyfwdddzxccdsssfayqjshsf= '否'  OR fwyfwdddzxccdsssfayqjshsf is null then 1 else 0 end)  fwyfwdddzxccdsssfayqjshsf_f,
                            sum(case when use_bottled_liquefied_gas= '是'   then 1 else 0 end) use_bottled_liquefied_gas_s,
                            sum(case when use_bottled_liquefied_gas= '否' OR use_bottled_liquefied_gas is null then 1 else 0 end)  use_bottled_liquefied_gas_f,
                            sum(case when use_stacked_items= '是' OR use_stacked_items is null then 1 else 0 end) use_stacked_items_s,
                            sum(case when use_stacked_items= '否'  then 1 else 0 end)  use_stacked_items_f,
                            sum(case when slljdx= '是' OR slljdx is null then 1 else 0 end) slljdx_s,
                            sum(case when slljdx= '否'  then 1 else 0 end)  slljdx_f,
                            sum(case when notes is not null then 1 else 0 end) notes_s,
                            sum(case when notes is null then 1 else 0 end)  notes_f from(select ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num, t1.xzq, t1.xzqdm, t1.towndm, t1.town, t1.villagedm, t1.village, t1.hzxm, t1.address, t1.fwbh, t1.cs, t1.jsrysstd, t1.create_on, t1.create_name, t1.yhly,case when t1.wdwhjgfjgqsyyrkrgbdj = 1 then '是' when t1.wdwhjgfjgqsyyrkrgbdj = '0' then '否' end wdwhjgfjgqsyyrkrgbdj,
                            case when t1.sstdsyyrkrclzx = 1 then '是' when t1.sstdsyyrkrclzx = '0' then '否' end sstdsyyrkrclzx,
                            case when t1.gryssdcsszblyyqksddp = 1 then '是' when t1.gryssdcsszblyyqksddp = '0' then '否' end gryssdcsszblyyqksddp,
                            case when t1.fwyfwdddzxccdsssfayqjshsf = 1 then '是' when t1.fwyfwdddzxccdsssfayqjshsf = '0' then '否' end fwyfwdddzxccdsssfayqjshsf,
                            case when use_electric_bicycle= 1 then '是' when use_electric_bicycle = '0' then '否' end use_electric_bicycle,
                            case when use_bottled_liquefied_gas= 1 then '是' when use_bottled_liquefied_gas = '0' then '否' end use_bottled_liquefied_gas,
                            case when use_stacked_items= 1 then '是' when use_stacked_items = '0' then '否' end use_stacked_items,
                            case when mhqwbcwhyx= 1 then '是' when mhqwbcwhyx = '0' then '否' end mhqwbcwhyx,
                            case when jyplmhxtwbcwhyx= '1' then '是' when jyplmhxtwbcwhyx = '0' then '否' end jyplmhxtwbcwhyx,
                            case when ygbjwfwhyx= '1' then '是' when ygbjwfwhyx = '0' then '否' end ygbjwfwhyx,
                            case when slljdx= '1' then '是' when slljdx = '0' then '否' end slljdx,
                            case when sfczckfhwggptzl= '1' then '是' when sfczckfhwggptzl = '0' then '否' end sfczckfhwggptzl, notes from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id = t2.base_id  and t1.yhly='" + yhly + "' and t1.create_on  BETWEEN '" + startTime + "'  and '" + endTime + " 23:59:59') t " +
                           " where row_num = 1 and yhly = '" + yhly + "' and create_on::TIMESTAMP BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP   and fwbh not in(select fwbh From "+table_fwzt+ " where final_create_on='" + endTime + " 23:59:59' and final_fwzt is not null )  " +
                           "  group by  xzq,xzqdm,town,village) t " +
                           "  left join zm_building_ledger k on t.villagedm=k.villagedm ) t right join (select DISTINCT xzq,xzqdm,town,towndm,village,villagedm from zm_building_ledger where xzqdm='" + xzqdm + "' and towndm='" + towndm + "' ) k on t.villagedm=k.villagedm  where  1=1 ) t" +
                           " left join (select  a.villagedm,count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='use_electric_bicycle' then 1 end) use_electric_bicycle_wzg,count(case when fh_zt=0 and a.check_id='use_electric_bicycle' and fh_time_dx::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP then 1 end)use_electric_bicycle_yzg ," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='use_bottled_liquefied_gas' then 1 end) use_bottled_liquefied_gas_wzg," +
                           "count(case when fh_zt=0 and a.check_id='use_bottled_liquefied_gas' and fh_time_dx::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP then 1 end)use_bottled_liquefied_gas_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='use_stacked_items' then 1 end) use_stacked_items_wzg," +
                           "count(case when fh_zt=0 and a.check_id='use_stacked_items' and fh_time_dx::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP then 1 end)use_stacked_items_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='mhqwbcwhyx' then 1 end) mhqwbcwhyx_wzg," +
                           "count(case when fh_zt=0 and a.check_id='mhqwbcwhyx' and fh_time_dx::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)mhqwbcwhyx_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='jyplmhxtwbcwhyx' then 1 end)jyplmhxtwbcwhyx_wzg," +
                           "count(case when fh_zt=0 and a.check_id='jyplmhxtwbcwhyx' and fh_time_dx::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP then 1 end)jyplmhxtwbcwhyx_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='ygbjwfwhyx' then 1 end)ygbjwfwhyx_wzg," +
                           "count(case when fh_zt=0 and a.check_id='ygbjwfwhyx' and fh_time_dx::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP then 1 end)ygbjwfwhyx_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='slljdx' then 1 end)slljdx_wzg," +
                           "count(case when fh_zt=0 and a.check_id='slljdx' and fh_time_dx::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP then 1 end)slljdx_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='sfczckfhwggptzl' then 1 end)sfczckfhwggptzl_wzg," +
                           "count(case when fh_zt=0 and a.check_id='sfczckfhwggptzl' and fh_time_dx::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP then 1 end)sfczckfhwggptzl_yzg from zm_pc_yhlog_dx_sqjz a  where a.jcly='" + yhly + "' and a.create_on::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP  GROUP BY a.villagedm)tt on t.villagedm=tt.villagedm)t " +
                           " where  1=1 " + strWhere;

            if (sql.Contains("insert into", StringComparison.OrdinalIgnoreCase)
                || sql.Contains("delete from", StringComparison.OrdinalIgnoreCase)
                || sql.Contains("update ", StringComparison.OrdinalIgnoreCase))
            {
                return new RespRet<Page<object>>(false, "不符合规范，禁止执行");
            }
            string sql_count = @"select  count(1) cnt  from (select t.*,
 COALESCE(tt.use_electric_bicycle_wzg, 0) use_electric_bicycle_wzg,
 COALESCE(tt.use_electric_bicycle_yzg, 0) use_electric_bicycle_yzg,
 COALESCE(tt.use_bottled_liquefied_gas_wzg, 0) use_bottled_liquefied_gas_wzg,
 COALESCE(tt.use_bottled_liquefied_gas_yzg, 0) use_bottled_liquefied_gas_yzg,
 COALESCE(tt.use_stacked_items_wzg, 0) use_stacked_items_wzg,
 COALESCE(tt.use_stacked_items_yzg, 0) use_stacked_items_yzg,
 COALESCE(tt.mhqwbcwhyx_wzg, 0) mhqwbcwhyx_wzg,
 COALESCE(tt.mhqwbcwhyx_yzg, 0) mhqwbcwhyx_yzg,
 COALESCE(tt.jyplmhxtwbcwhyx_wzg, 0) jyplmhxtwbcwhyx_wzg,
 COALESCE(tt.jyplmhxtwbcwhyx_yzg, 0) jyplmhxtwbcwhyx_yzg,
 COALESCE(tt.ygbjwfwhyx_wzg, 0) ygbjwfwhyx_wzg,
 COALESCE(tt.ygbjwfwhyx_yzg, 0) ygbjwfwhyx_yzg,
 COALESCE(tt.slljdx_wzg, 0) slljdx_wzg,
 COALESCE(tt.slljdx_yzg, 0) slljdx_yzg,
 COALESCE(tt.sfczckfhwggptzl_wzg, 0) sfczckfhwggptzl_wzg,
 COALESCE(tt.sfczckfhwggptzl_yzg, 0) sfczckfhwggptzl_yzg 
 from (select  	k.xzq,k.xzqdm,k.town,k.towndm,k.village,k.villagedm, COALESCE(cnt, 0) AS cnt,COALESCE(jsrysstd_s, 0) AS jsrysstd_s,COALESCE(jsrysstd_f, 0) AS jsrysstd_f,COALESCE(wdwhjgfjgqsyyrkrgbdj_s, 0) AS wdwhjgfjgqsyyrkrgbdj_s,COALESCE(wdwhjgfjgqsyyrkrgbdj_f, 0) AS wdwhjgfjgqsyyrkrgbdj_f,COALESCE(sfczckfhwggptzl_s, 0) AS sfczckfhwggptzl_s,	COALESCE(sfczckfhwggptzl_f, 0) AS sfczckfhwggptzl_f,COALESCE(use_electric_bicycle_s, 0) AS use_electric_bicycle_s,COALESCE(use_electric_bicycle_f, 0) AS use_electric_bicycle_f,COALESCE(fwyfwdddzxccdsssfayqjshsf_s, 0) AS fwyfwdddzxccdsssfayqjshsf_s,COALESCE(fwyfwdddzxccdsssfayqjshsf_f, 0) AS fwyfwdddzxccdsssfayqjshsf_f,COALESCE(use_bottled_liquefied_gas_s, 0) AS use_bottled_liquefied_gas_s,COALESCE(use_bottled_liquefied_gas_f, 0) AS use_bottled_liquefied_gas_f,COALESCE(use_stacked_items_s, 0) AS use_stacked_items_s,COALESCE(use_stacked_items_f, 0) AS use_stacked_items_f,
							COALESCE(slljdx_s, 0) AS slljdx_s,COALESCE(slljdx_f, 0) AS slljdx_f,COALESCE(notes_s, 0) AS notes_s,COALESCE(notes_f, 0) AS notes_f from (select t.*,k.town ,k.village
                             from(select xzqdm,xzq,town towndm,village villagedm,count(1)cnt,
                            sum(case when jsrysstd= '是'   then 1 else 0 end)  jsrysstd_s,
                            sum(case when jsrysstd= '否' OR jsrysstd is null then 1 else 0 end)  jsrysstd_f,
                            sum(case when wdwhjgfjgqsyyrkrgbdj= '是'  then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_s,
                            sum(case when wdwhjgfjgqsyyrkrgbdj= '否' OR wdwhjgfjgqsyyrkrgbdj is null  then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_f,
                            sum(case when sfczckfhwggptzl= '是'  then 1 else 0 end)  sfczckfhwggptzl_s,
                            sum(case when sfczckfhwggptzl= '否' OR sfczckfhwggptzl is null  then 1 else 0 end)  sfczckfhwggptzl_f,
                            sum(case when use_electric_bicycle= '是'  then 1 else 0 end) use_electric_bicycle_s,
                            sum(case when use_electric_bicycle= '否' OR use_electric_bicycle is null  then 1 else 0 end)  use_electric_bicycle_f,
                            sum(case when fwyfwdddzxccdsssfayqjshsf= '是'  then 1 else 0 end) fwyfwdddzxccdsssfayqjshsf_s,
                            sum(case when fwyfwdddzxccdsssfayqjshsf= '否'  OR fwyfwdddzxccdsssfayqjshsf is null then 1 else 0 end)  fwyfwdddzxccdsssfayqjshsf_f,
                            sum(case when use_bottled_liquefied_gas= '是'   then 1 else 0 end) use_bottled_liquefied_gas_s,
                            sum(case when use_bottled_liquefied_gas= '否' OR use_bottled_liquefied_gas is null then 1 else 0 end)  use_bottled_liquefied_gas_f,
                            sum(case when use_stacked_items= '是' OR use_stacked_items is null then 1 else 0 end) use_stacked_items_s,
                            sum(case when use_stacked_items= '否'  then 1 else 0 end)  use_stacked_items_f,
                            sum(case when slljdx= '是' OR slljdx is null then 1 else 0 end) slljdx_s,
                            sum(case when slljdx= '否'  then 1 else 0 end)  slljdx_f,
                            sum(case when notes is not null then 1 else 0 end) notes_s,
                            sum(case when notes is null then 1 else 0 end)  notes_f from(select ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num, t1.xzq, t1.xzqdm, t1.towndm, t1.town, t1.villagedm, t1.village, t1.hzxm, t1.address, t1.fwbh, t1.cs, t1.jsrysstd, t1.create_on, t1.create_name, t1.yhly,case when t1.wdwhjgfjgqsyyrkrgbdj = 1 then '是' when t1.wdwhjgfjgqsyyrkrgbdj = '0' then '否' end wdwhjgfjgqsyyrkrgbdj,
                            case when t1.sstdsyyrkrclzx = 1 then '是' when t1.sstdsyyrkrclzx = '0' then '否' end sstdsyyrkrclzx,
                            case when t1.gryssdcsszblyyqksddp = 1 then '是' when t1.gryssdcsszblyyqksddp = '0' then '否' end gryssdcsszblyyqksddp,
                            case when t1.fwyfwdddzxccdsssfayqjshsf = 1 then '是' when t1.fwyfwdddzxccdsssfayqjshsf = '0' then '否' end fwyfwdddzxccdsssfayqjshsf,
                            case when use_electric_bicycle= 1 then '是' when use_electric_bicycle = '0' then '否' end use_electric_bicycle,
                            case when use_bottled_liquefied_gas= 1 then '是' when use_bottled_liquefied_gas = '0' then '否' end use_bottled_liquefied_gas,
                            case when use_stacked_items= 1 then '是' when use_stacked_items = '0' then '否' end use_stacked_items,
                            case when mhqwbcwhyx= 1 then '是' when mhqwbcwhyx = '0' then '否' end mhqwbcwhyx,
                            case when jyplmhxtwbcwhyx= '1' then '是' when jyplmhxtwbcwhyx = '0' then '否' end jyplmhxtwbcwhyx,
                            case when ygbjwfwhyx= '1' then '是' when ygbjwfwhyx = '0' then '否' end ygbjwfwhyx,
                            case when slljdx= '1' then '是' when slljdx = '0' then '否' end slljdx,
                            case when sfczckfhwggptzl= '1' then '是' when sfczckfhwggptzl = '0' then '否' end sfczckfhwggptzl, notes from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id = t2.base_id  and t1.yhly='" + yhly + "' and t1.create_on  BETWEEN '" + startTime + "'  and '" + endTime + " 23:59:59') t " +
                           " where row_num = 1 and yhly = '" + yhly + "' and create_on::TIMESTAMP BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP   and fwbh not in(select fwbh From "+table_fwzt+ " where final_create_on='" + endTime + " 23:59:59' and final_fwzt is not null )  " +
                           "   group by  xzq,xzqdm,town,village) t " +
                           " left join zm_building_ledger k on t.villagedm=k.villagedm ) t right join (select DISTINCT xzq,xzqdm,town,towndm,village,villagedm from zm_building_ledger where xzqdm='" + xzqdm + "' and towndm='" + towndm + "' ) k on t.villagedm=k.villagedm  where  1=1 ) t" +
                           " left join (select  a.villagedm,count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='use_electric_bicycle' then 1 end) use_electric_bicycle_wzg,count(case when fh_zt=0 and a.check_id='use_electric_bicycle' and fh_time_dx::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP then 1 end)use_electric_bicycle_yzg ," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='use_bottled_liquefied_gas' then 1 end) use_bottled_liquefied_gas_wzg," +
                           "count(case when fh_zt=0 and a.check_id='use_bottled_liquefied_gas' and fh_time_dx::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP then 1 end)use_bottled_liquefied_gas_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='use_stacked_items' then 1 end) use_stacked_items_wzg," +
                           "count(case when fh_zt=0 and a.check_id='use_stacked_items' and fh_time_dx::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP then 1 end)use_stacked_items_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='mhqwbcwhyx' then 1 end) mhqwbcwhyx_wzg," +
                           "count(case when fh_zt=0 and a.check_id='mhqwbcwhyx' and fh_time_dx::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP  then 1 end)mhqwbcwhyx_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='jyplmhxtwbcwhyx' then 1 end)jyplmhxtwbcwhyx_wzg," +
                           "count(case when fh_zt=0 and a.check_id='jyplmhxtwbcwhyx' and fh_time_dx::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP then 1 end)jyplmhxtwbcwhyx_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='ygbjwfwhyx' then 1 end)ygbjwfwhyx_wzg," +
                           "count(case when fh_zt=0 and a.check_id='ygbjwfwhyx' and fh_time_dx::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP then 1 end)ygbjwfwhyx_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='slljdx' then 1 end)slljdx_wzg," +
                           "count(case when fh_zt=0 and a.check_id='slljdx' and fh_time_dx::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP then 1 end)slljdx_yzg," +
                           "count(case when (fh_zt !=0 or fh_zt is null) and a.check_id='sfczckfhwggptzl' then 1 end)sfczckfhwggptzl_wzg," +
                           "count(case when fh_zt=0 and a.check_id='sfczckfhwggptzl' and fh_time_dx::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP then 1 end)sfczckfhwggptzl_yzg from zm_pc_yhlog_dx_sqjz a  where a.jcly='" + yhly + "' and a.create_on::TIMESTAMP  BETWEEN '" + startTime + "'::TIMESTAMP and '" + endTime + " 23:59:59'::TIMESTAMP  GROUP BY a.villagedm)tt on t.villagedm=tt.villagedm)t " +
                           " where  1=1 " + strWhere;
            if (sql_count.Contains("insert into", StringComparison.OrdinalIgnoreCase)
                || sql_count.Contains("delete from", StringComparison.OrdinalIgnoreCase)
                || sql_count.Contains("update ", StringComparison.OrdinalIgnoreCase))
            {
                return new RespRet<Page<object>>(false, "不符合规范，禁止执行");
            }
            int count = _dbContext.Database.GetDbConnection().QueryFirstOrDefault<int>(sql_count);
            var datas = _dbContext.Database.GetDbConnection().Query<object>(sql).ToList();
            return new RespRet<Page<object>>(true, "ok", new Page<object> { Items = datas, NextPageIndex = pageindex + 1, PageTotal = count, Total = count });
        }











        /// <summary>
        /// 导出隐患信息
        /// </summary>
        /// <param name="pageRequest">type check</param>
        /// <returns></returns>
        [HttpGet("export/other/Exportlist")]
        [Produces("application/octet-stream")]
        //[ProducesResponseType(200)]
        //[ProducesResponseType(401)]
        public IActionResult Exportlist([FromQuery] string startTime, [FromQuery] string endTime, [FromQuery] string yhly, [FromQuery] string xzqdm, [FromQuery] string towndm, [FromQuery] string villagedm)
        {
            try
            {

                var userNumber = this.CurrentUser();
                string strWhere = "";
                var user = _userRepository.FirstOrDefault(u => u.Number == userNumber && u.IsDelete != 1);
                string district = user.District;
                var options = new JsonSerializerOptions();
                options.PropertyNameCaseInsensitive = true; // 设置属性不区分大小写
                Xzq model = System.Text.Json.JsonSerializer.Deserialize<Xzq>(district, options);


                if (model != null)
                {
                    if (user.RoleType == 1 || user.RoleType == 2)
                    {
                        strWhere += "and  xzq!='东城区' and xzq!='西城区'";
                    }
                    else if (user.RoleType == 3) //区级
                    {
                        strWhere += " and xzqdm='" + model.RegionCode + "'";
                    }
                    else  //乡镇
                    {
                        strWhere += "and xzqdm='" + model.RegionCode.Substring(0, 6) + "' ";
                    }
                }


                if (string.IsNullOrEmpty(startTime) && string.IsNullOrEmpty(endTime))
                {
                    startTime = DateTime.Now.ToShortDateString();
                    endTime = DateTime.Now.ToShortDateString();
                }
              
                if (!string.IsNullOrEmpty(xzqdm) && !string.IsNullOrEmpty(towndm) && !string.IsNullOrEmpty(villagedm))
                {
                    strWhere += " and xzqdm='" + xzqdm + "' and town='" + towndm + "' and village='" + villagedm + "'";
                }
                else if (!string.IsNullOrEmpty(xzqdm) && !string.IsNullOrEmpty(towndm) && string.IsNullOrEmpty(villagedm))
                {
                    strWhere += " and xzqdm='" + xzqdm + "' and town='" + towndm + "' ";
                }
                else if (!string.IsNullOrEmpty(xzqdm) && string.IsNullOrEmpty(towndm) && string.IsNullOrEmpty(villagedm))
                {
                    strWhere += " and xzqdm='" + xzqdm + "' ";
                }

                if (string.IsNullOrEmpty(yhly))
                {
                    yhly = "0";
                }

                string toDay = DateTime.Now.ToString("yyyy-MM-dd");
                string table_fwzt = "";
                if (!string.IsNullOrEmpty(endTime))
                {
                    DateTime endData = Convert.ToDateTime(endTime);
                    table_fwzt = "zm_building_fwzt_" + endData.Year;
                }
                else
                {
                    DateTime endData = Convert.ToDateTime(toDay);
                    table_fwzt = "zm_building_fwzt_" + endData.Year;
                }
                string sql = @"select  ROW_NUMBER() OVER ()index,*  from (select  	k.xzq,k.xzqdm,COALESCE(cnt, 0) AS cnt,COALESCE(jsrysstd_s, 0) AS jsrysstd_s,COALESCE(jsrysstd_f, 0) AS jsrysstd_f,COALESCE(wdwhjgfjgqsyyrkrgbdj_s, 0) AS wdwhjgfjgqsyyrkrgbdj_s,COALESCE(wdwhjgfjgqsyyrkrgbdj_f, 0) AS wdwhjgfjgqsyyrkrgbdj_f,COALESCE(sfczckfhwggptzl_s, 0) AS sfczckfhwggptzl_s,	COALESCE(sfczckfhwggptzl_f, 0) AS sfczckfhwggptzl_f,COALESCE(use_electric_bicycle_s, 0) AS use_electric_bicycle_s,COALESCE(use_electric_bicycle_f, 0) AS use_electric_bicycle_f,COALESCE(fwyfwdddzxccdsssfayqjshsf_s, 0) AS fwyfwdddzxccdsssfayqjshsf_s,COALESCE(fwyfwdddzxccdsssfayqjshsf_f, 0) AS fwyfwdddzxccdsssfayqjshsf_f,COALESCE(use_bottled_liquefied_gas_s, 0) AS use_bottled_liquefied_gas_s,COALESCE(use_bottled_liquefied_gas_f, 0) AS use_bottled_liquefied_gas_f,COALESCE(use_stacked_items_s, 0) AS use_stacked_items_s,COALESCE(use_stacked_items_f, 0) AS use_stacked_items_f,
							COALESCE(slljdx_s, 0) AS slljdx_s,COALESCE(slljdx_f, 0) AS slljdx_f,COALESCE(notes_s, 0) AS notes_s,COALESCE(notes_f, 0) AS notes_f from (select  xzqdm,xzq,count(1) cnt,
                            sum(case when jsrysstd= '是' OR jsrysstd is null then 1 else 0 end)  jsrysstd_s,
                            sum(case when jsrysstd= '否'  then 1 else 0 end)  jsrysstd_f,
                            sum(case when wdwhjgfjgqsyyrkrgbdj= '是'  OR wdwhjgfjgqsyyrkrgbdj is null  then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_s,
                            sum(case when wdwhjgfjgqsyyrkrgbdj= '否'then 1 else 0 end)  wdwhjgfjgqsyyrkrgbdj_f,
                            sum(case when sfczckfhwggptzl= '是'  then 1 else 0 end)  sfczckfhwggptzl_s,
                            sum(case when sfczckfhwggptzl= '否'  then 1 else 0 end)  sfczckfhwggptzl_f,
                            sum(case when use_electric_bicycle= '是'  then 1 else 0 end) use_electric_bicycle_s,
                            sum(case when use_electric_bicycle= '否' then 1 else 0 end)  use_electric_bicycle_f,
                            sum(case when fwyfwdddzxccdsssfayqjshsf= '是' OR fwyfwdddzxccdsssfayqjshsf is null then 1 else 0 end) fwyfwdddzxccdsssfayqjshsf_s,
                            sum(case when fwyfwdddzxccdsssfayqjshsf= '否'  then 1 else 0 end)  fwyfwdddzxccdsssfayqjshsf_f,
                            sum(case when use_bottled_liquefied_gas= '是' then 1 else 0 end) use_bottled_liquefied_gas_s,
                            sum(case when use_bottled_liquefied_gas= '否' OR use_bottled_liquefied_gas is null then 1 else 0 end)  use_bottled_liquefied_gas_f,
                            sum(case when use_stacked_items= '是' then 1 else 0 end) use_stacked_items_s,
                            sum(case when use_stacked_items= '否' OR use_stacked_items is null then 1 else 0 end)  use_stacked_items_f,
                            sum(case when slljdx= '是' then 1 else 0 end) slljdx_s,
                            sum(case when slljdx= '否' OR slljdx is null then 1 else 0 end)  slljdx_f,
                            sum(case when notes is not null then 1 else 0 end) notes_s,
                            sum(case when notes is null then 1 else 0 end)  notes_f
                             from(select * from(select ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num, t1.xzq, t1.xzqdm, t1.towndm, t1.town, t1.villagedm, t1.village, t1.hzxm, t1.address, t1.fwbh, t1.cs, t1.jsrysstd, t1.create_on, t1.create_name, t1.yhly,case when t1.wdwhjgfjgqsyyrkrgbdj = 1 then '是' when t1.wdwhjgfjgqsyyrkrgbdj = '0' then '否' end wdwhjgfjgqsyyrkrgbdj,
                            case when t1.sstdsyyrkrclzx = 1 then '是' when t1.sstdsyyrkrclzx = '0' then '否' end sstdsyyrkrclzx,
                            case when t1.gryssdcsszblyyqksddp = 1 then '是' when t1.gryssdcsszblyyqksddp = '0' then '否' end gryssdcsszblyyqksddp,
                            case when t1.fwyfwdddzxccdsssfayqjshsf = 1 then '是' when t1.fwyfwdddzxccdsssfayqjshsf = '0' then '否' end fwyfwdddzxccdsssfayqjshsf,
                            case when use_electric_bicycle= 1 then '是' when use_electric_bicycle = '0' then '否' end use_electric_bicycle,
                            case when use_bottled_liquefied_gas= 1 then '是' when use_bottled_liquefied_gas = '0' then '否' end use_bottled_liquefied_gas,
                            case when use_stacked_items= 1 then '是' when use_stacked_items = '0' then '否' end use_stacked_items,
                            case when mhqwbcwhyx= 1 then '是' when mhqwbcwhyx = '0' then '否' end mhqwbcwhyx,
                            case when jyplmhxtwbcwhyx= '1' then '是' when jyplmhxtwbcwhyx = '0' then '否' end jyplmhxtwbcwhyx,
                            case when ygbjwfwhyx= '1' then '是' when ygbjwfwhyx = '0' then '否' end ygbjwfwhyx,
                            case when slljdx= '1' then '是' when slljdx = '0' then '否' end slljdx,
                            case when sfczckfhwggptzl= '1' then '是' when sfczckfhwggptzl = '0' then '否' end sfczckfhwggptzl, notes from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id = t2.base_id and t1.yhly='" + yhly + "' and t1.create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59' ) t " +
                               " where row_num = 1 and yhly = '" + yhly + "' and create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59'  and fwbh not in (select distinct fwbh from "+table_fwzt+"  where final_fwzt is not null and   final_create_on='"+endTime+" 23:59:59')   order by create_on desc, xzqdm, village asc ) t group by xzq, xzqdm order by xzqdm) t right join (select DISTINCT xzq,xzqdm from zm_building_ledger) k on t.xzqdm=k.xzqdm  where  1=1 ) t  where  1=1 " + strWhere + " order by xzqdm";


                var data = _dbContext.Database.GetDbConnection().Query<object>(sql).ToList();
                IWorkbook wb = new XSSFWorkbook();
                ISheet sheet = wb.CreateSheet("核验信息");

                ICellStyle mainHeaderStyle = wb.CreateCellStyle();
                mainHeaderStyle.Alignment = HorizontalAlignment.Center;
                // 第一行 标题
                IRow row = sheet.CreateRow(0);
                row.CreateCell(0).SetCellValue("序号");
                row.CreateCell(1).SetCellValue("行政区名称");
                row.CreateCell(2).SetCellValue("已核验数量");
                row.CreateCell(3).SetCellValue("是否满足“双通道”要求");
                row.CreateCell(4).SetCellValue("");
                row.CreateCell(5).SetCellValue("是否存在彩钢板建筑出租住人");
                row.CreateCell(6).SetCellValue("");
                row.CreateCell(7).SetCellValue("是否存在窗口防护网、广告牌、铁栅栏");
                row.CreateCell(8).SetCellValue("");
                row.CreateCell(9).SetCellValue("电动自行车及蓄电池是否在室内停放充电");
                row.CreateCell(10).SetCellValue("");
                row.CreateCell(11).SetCellValue("服务于房屋的电动自行车充电设施是否按要求建设");
                row.CreateCell(12).SetCellValue("");
                row.CreateCell(13).SetCellValue("是否违规使用液化石油气瓶");
                row.CreateCell(14).SetCellValue("");
                row.CreateCell(15).SetCellValue("疏散通道是否堆放杂物");
                row.CreateCell(16).SetCellValue("");
                row.CreateCell(17).SetCellValue("是否私拉乱接电线");
                row.CreateCell(18).SetCellValue("");
                row.CreateCell(19).SetCellValue("其他高风险火灾隐患问题");
                row.CreateCell(20).SetCellValue("");

                // 合并单元格创建多级标题
                sheet.AddMergedRegion(new CellRangeAddress(0, 0, 3, 4)); // 合并
                sheet.AddMergedRegion(new CellRangeAddress(0, 0, 5, 6)); // 合并
                sheet.AddMergedRegion(new CellRangeAddress(0, 0, 7, 8)); // 合并
                sheet.AddMergedRegion(new CellRangeAddress(0, 0, 9, 10)); // 合并
                sheet.AddMergedRegion(new CellRangeAddress(0, 0, 11, 12)); // 合并
                sheet.AddMergedRegion(new CellRangeAddress(0, 0, 13, 14)); // 合并
                sheet.AddMergedRegion(new CellRangeAddress(0, 0, 15, 16)); // 合并
                sheet.AddMergedRegion(new CellRangeAddress(0, 0, 17, 18)); // 合并
                sheet.AddMergedRegion(new CellRangeAddress(0, 0, 19, 20)); // 合并
                IRow row1 = sheet.CreateRow(1);
                row1.CreateCell(0).SetCellValue("");
                row1.CreateCell(1).SetCellValue("");
                row1.CreateCell(2).SetCellValue("");
                row1.CreateCell(3).SetCellValue("已满足");
                row1.CreateCell(4).SetCellValue("未满足");

                row1.CreateCell(5).SetCellValue("已存在");
                row1.CreateCell(6).SetCellValue("不存在");

                row1.CreateCell(7).SetCellValue("已存在");
                row1.CreateCell(8).SetCellValue("不存在");

                row1.CreateCell(9).SetCellValue("是");
                row1.CreateCell(10).SetCellValue("否");

                row1.CreateCell(11).SetCellValue("是");
                row1.CreateCell(12).SetCellValue("否");

                row1.CreateCell(13).SetCellValue("是");
                row1.CreateCell(14).SetCellValue("否");
                
                row1.CreateCell(15).SetCellValue("是");
                row1.CreateCell(16).SetCellValue("否");
               
                row1.CreateCell(17).SetCellValue("是");
                row1.CreateCell(18).SetCellValue("否");

                row1.CreateCell(19).SetCellValue("是");
                row1.CreateCell(20).SetCellValue("否");

                // 第二行 写数据
                int i = 2;
                foreach (object obj in data)
                {
                    Dictionary<string, object> dictionary = new Dictionary<string, object>();
                    if (obj != null)
                    {

                        var properties = obj as IDictionary<string, object>;
                        row = sheet.CreateRow(i);
                        foreach (var kvp in properties)
                        {
                            //var val = kvp.Value+"";
                            if (kvp.Key == "index")
                            {
                                row.CreateCell(0).SetCellValue(i - 1);
                            }
                            else if (kvp.Key == "xzq")
                            {
                                row.CreateCell(1).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "town")
                            {
                                row.CreateCell(1).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "village")
                            {
                                row.CreateCell(1).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "cnt")
                            {
                                row.CreateCell(2).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "jsrysstd_s")
                            {
                                row.CreateCell(3).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "jsrysstd_f")
                            {
                                row.CreateCell(4).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "wdwhjgfjgqsyyrkrgbdj_s")
                            {
                                row.CreateCell(5).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "wdwhjgfjgqsyyrkrgbdj_f")
                            {
                                row.CreateCell(6).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "sfczckfhwggptzl_s")
                            {
                                row.CreateCell(7).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "sfczckfhwggptzl_f")
                            {
                                row.CreateCell(8).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "use_electric_bicycle_s")
                            {
                                row.CreateCell(9).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "use_electric_bicycle_f")
                            {
                                row.CreateCell(10).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "fwyfwdddzxccdsssfayqjshsf_s")
                            {
                                row.CreateCell(11).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "fwyfwdddzxccdsssfayqjshsf_f")
                            {
                                row.CreateCell(12).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "use_bottled_liquefied_gas_s")
                            {
                                row.CreateCell(13).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "use_bottled_liquefied_gas_f")
                            {
                                row.CreateCell(14).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "use_stacked_items_s")
                            {
                                row.CreateCell(15).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "use_stacked_items_f")
                            {
                                row.CreateCell(16).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "slljdx_s")
                            {
                                row.CreateCell(17).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "slljdx_f")
                            {
                                row.CreateCell(18).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "notes_s")
                            {
                                row.CreateCell(19).SetCellValue(kvp.Value + "");
                            }
                            else if (kvp.Key == "notes_f")
                            {
                                row.CreateCell(20).SetCellValue(kvp.Value + "");
                            }
                         
                        }

                        i++;
                    }
                }

                // 写 WorkBook信息到 内存流中
                byte[] buffer = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    wb.Write(ms);
                    buffer = ms.ToArray();
                }
              
                return File(buffer, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "核验统计" + DateTime.Now.ToString("yyyyMMdd") + ".xlsx");
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 删除房屋
        /// </summary>
        /// <param name="fwbh">用户唯一编号</param>
        /// <returns></returns>
        [HttpGet("hourse/deleteHourse")]
        [Produces("application/json")]
        public RespRet<Result> deleteHourse([FromQuery] string fwbh)
        {
            if (string.IsNullOrEmpty(fwbh))
            {
                return new RespRet<Result>(false, "fwbh not allow empty");
            }

            var adminUserNumber = this.CurrentUser();
            if (string.IsNullOrEmpty(adminUserNumber))
            {
                return new RespRet<Result>(false, "user not logined");
            }
            string sqlstr = "";
            string today = DateTime.Now.ToString("yyyy-MM-dd");
            DateTime endData = Convert.ToDateTime(today);
            string table_fwzt = "zm_building_fwzt_" + endData.Year;
            string sql = "select * from zm_pc_base_cj where fwbh ='" + fwbh + "'";
            var datas = _dbContext.Database.GetDbConnection().Query<object>(sql).ToList();

            string time = DateTime.Today
    .AddHours(23)
    .AddMinutes(59)
    .AddSeconds(59)
    .ToString("yyyy-MM-dd HH:mm:ss");

            if (datas != null && datas.Count > 0)
            {
                string update_sql = " update zm_pc_base_cj set delete_by='删除',delete_on='" + DateTime.Now + "',delete_ip='" + HttpContext.Connection.RemoteIpAddress?.ToString() + "',fwzt='3' where fwbh='" + fwbh + "'";
                _dbContext.Database.GetDbConnection().Query<object>(update_sql).ToList();


                string updatelog_sql = " update zm_pc_baselog_cj set delete_by='删除',delete_on='" + DateTime.Now + "',delete_ip='" + HttpContext.Connection.RemoteIpAddress?.ToString() + "',fwzt='3' where id in(select id from zm_pc_base_cj where fwbh ='" + fwbh + "')";
                _dbContext.Database.GetDbConnection().Query<object>(updatelog_sql).ToList();


                string update_fwzt = " update " + table_fwzt + " set final_fwzt='3' where  fwbh='" + fwbh + "'  and  final_create_on='" + time + "'  ";
                _dbContext.Database.GetDbConnection().Query<object>(update_fwzt).ToList();

            }
            else
            {
                sql = "select * from zm_pc_base_sqjz where fwbh ='" + fwbh + "'";
                datas = _dbContext.Database.GetDbConnection().Query<object>(sql).ToList();
                if (datas != null && datas.Count > 0)
                {
                    string update_sql = " update zm_pc_base_sqjz set delete_by='删除',delete_on='" + DateTime.Now + "',delete_ip='" + HttpContext.Connection.RemoteIpAddress?.ToString() + "',fwzt='3' where fwbh='" + fwbh + "' and yhly='4'";
                    _dbContext.Database.GetDbConnection().Query<object>(update_sql).ToList();


                    string updatelog_sql = " update zm_pc_baselog_sqjz set delete_by='删除',delete_on='" + DateTime.Now + "',delete_ip='" + HttpContext.Connection.RemoteIpAddress?.ToString() + "',fwzt='3' where id in(select id from zm_pc_base_sqjz where fwbh ='" + fwbh + "' and yhly='4')";
                    _dbContext.Database.GetDbConnection().Query<object>(updatelog_sql).ToList();

                    string update_fwzt = " update " + table_fwzt + " set final_fwzt='3' where  fwbh='" + fwbh + "'  and  final_create_on='" + time + "'  ";
                    _dbContext.Database.GetDbConnection().Query<object>(update_fwzt).ToList();
                }
            }

            string u_disable = "update zm_user set disabled =1 where \"number\" = '" + fwbh+"'";

            var user = _dbContext.Database.GetDbConnection().Query<object>(u_disable).ToList();

            return new RespRet<Result>(true, "Ok|" + user);
        }

        /// <summary>
        /// 核验明细
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpGet("hc/evolvestatic/getHcHourseMxDetail")]
        public RespRet<Page<object>> getHcHourseMxDetail([FromQuery] string xzqdm, [FromQuery] string towndm, [FromQuery] string villagedm, [FromQuery] string category, [FromQuery] int pageIndex, [FromQuery] int pageSize, [FromQuery] string endTime, [FromQuery] string startTime, [FromQuery] string txtKeyWord, [FromQuery] string cs, [FromQuery] string role_type)
        {
            if (pageIndex < 0)
            {
                return new RespRet<Page<object>>(false, "PageIndex must >= 0");
            }

            var userNumber = this.CurrentUser();
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<Page<object>>(false, "user not logined");
            }

            var user = _userRepository.FirstOrDefault(u => u.Number == userNumber && u.IsDelete != 1);
            if (null == user)
            {
                return new RespRet<Page<object>>(false, "用户名不存在");
            }

            var sbSql = new StringBuilder();
            var sbSqlWhere = new StringBuilder();
            var sqlParams = new List<NpgsqlParameter>();

            var sbSqlBuildingEx = new StringBuilder();

            string district = user.District;

            var options = new JsonSerializerOptions();
            options.PropertyNameCaseInsensitive = true; // 设置属性不区分大小写
            Xzq model = System.Text.Json.JsonSerializer.Deserialize<Xzq>(district, options);

            string strWhere = " ";
            if (!string.IsNullOrEmpty(txtKeyWord))
            {
                strWhere += " and (hzxm like '%" + txtKeyWord + "%' or fwbh like '%" + txtKeyWord + "%' or address like '%" + txtKeyWord + "%')";
            }

            if (!string.IsNullOrEmpty(xzqdm))
            {
                strWhere += " and xzqdm='" + xzqdm + "' ";
            }
            if (!string.IsNullOrEmpty(towndm))
            {
                strWhere += "  and towndm='" + towndm + "' ";
            }
            if (!string.IsNullOrEmpty(villagedm))
            {
                strWhere += " and villagedm='" + villagedm + "'";
            }
            if (string.IsNullOrEmpty(startTime) && !string.IsNullOrEmpty(endTime))
            {
                startTime = "2024-01-01";
                endTime = DateTime.Now.ToString("yyyy-MM-dd");
            }
            string sql = " ";
            string today = DateTime.Now.ToString("yyyy-MM-dd");
            string table_fwzt = "";
            if (!string.IsNullOrEmpty(endTime))
            {
                DateTime endData = Convert.ToDateTime(endTime);
                table_fwzt = "zm_building_fwzt_" + endData.Year;
            }
            else
            {
                DateTime endData = Convert.ToDateTime(today);
                table_fwzt = "zm_building_fwzt_" + endData.Year;
            }
            if (!string.IsNullOrEmpty(cs))
            {
                if (cs.Contains("3"))
                {
                    string[] arrr = cs.Split(',');
                    if (arrr.Length == 2)
                    {
                        string cssql = "";
                        for (int i = 0; i < arrr.Length; i++)
                        {
                            if (arrr[i] == "3")
                            {
                                cssql += "  cs >=3 OR ";
                            }
                            if (arrr[i] == "1")
                            {
                                cssql += "  cs=1 OR ";
                            }
                            if (arrr[i] == "2")
                            {
                                cssql += " cs =2 OR ";
                            }
                        }

                        strWhere += " and (" + (cssql.Substring(0, cssql.Length - 3)) + ")";
                    }
                    else
                    {
                        strWhere += " and cs >=" + cs;
                    }
                }
                else
                {
                    strWhere += " and cs in(" + cs + ")";
                }
            }
            DateTime start = Convert.ToDateTime(startTime);
            DateTime end = Convert.ToDateTime(endTime);
            if (category == "yhy")
            {

                    sql = @"select ROW_NUMBER() OVER ()index ,* from (select ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.xzq,t1.xzqdm,t1.town towndm,t1.towndm town,t1.villagedm village,t1.village villagedm,t1.hzxm,t1.address,t1.fwbh,t1.create_on from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id=t2.base_id and t1.yhly='" + role_type + "' and t1.create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59') t1 " +
             "    where  create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59' and row_num=1  and fwbh not in " +
             " (select distinct fwbh from " + table_fwzt + " where final_fwzt IS NOT NULL AND final_create_on = '" + endTime + " 23:59:59') " + strWhere + "limit " + pageSize + " offset(" + (pageIndex-1) + ") * " + pageSize;
              
            }
            else if (category == "wzc")
            {

                    sql = @"select  ROW_NUMBER() OVER ()index,*  from (select t1.xzq,t1.xzqdm,t1.town,t1.towndm,t1.villagedm,t1.village,t1.hzxm,t1.address,t1.fwbh  " +
                     " from zm_buildings t1 where  fwbh not in(select fwbh from " + table_fwzt + " where final_fwzt IS NOT NULL AND final_create_on = '" + endTime + " 23:59:59') " +
                    "  and fwbh not in(select fwbh from (select ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.fwbh,t1.xzqdm,t1.town towndm,t1.village villagedm,t1.create_on from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id=t2.base_id and t1.yhly='" + role_type + "' and t1.create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59') t1 " +
             "    where  create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59' and row_num=1  and fwbh not in " +
             " (select distinct fwbh from " + table_fwzt + " where final_fwzt IS NOT NULL AND final_create_on = '" + endTime + " 23:59:59') " + strWhere + ")  order by village ) t where  1=1  " + strWhere + " limit " + pageSize + " offset(" + (pageIndex-1) + ") * " + pageSize;
               

            }


            if (sql.Contains("insert into", StringComparison.OrdinalIgnoreCase)
          || sql.Contains("delete from", StringComparison.OrdinalIgnoreCase)
          || sql.Contains("update ", StringComparison.OrdinalIgnoreCase))
            {
                return new RespRet<Page<object>>(false, "不符合规范，禁止执行");
            }


            string sql_count = " ";

            if (category == "yhy")
            {
                sql_count = @"select count(1) cnt from (select ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.xzq,t1.xzqdm,t1.town towndm,t1.towndm town,t1.villagedm village,t1.village villagedm,t1.hzxm,t1.address,t1.fwbh,t1.create_on from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id=t2.base_id and t1.yhly='" + role_type + "') t1 " +
       "    where  create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59' and row_num=1  and fwbh not in " +
       " (select fwbh from " + table_fwzt + " where final_fwzt IS NOT NULL AND final_create_on = '" + endTime + " 23:59:59') " + strWhere;

            }
            else if (category == "wzc")
            {

                sql_count = @"select  count(1) cnt  from (select t1.xzq,t1.xzqdm,t1.town,t1.towndm,t1.villagedm,t1.village,t1.hzxm,t1.address,t1.fwbh  " +
                 " from zm_buildings t1 where  fwbh not in(select fwbh from " + table_fwzt + " where final_fwzt IS NOT NULL AND final_create_on = '" + endTime + " 23:59:59') " +
                "  and fwbh not in(select fwbh from (select ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.fwbh,t1.xzqdm,t1.town towndm,t1.village villagedm,t1.create_on from zm_pc_baselog_sqjz t1,zm_pc_yhlog_sqjz t2 where t1.id=t2.base_id and t1.yhly='" + role_type + "' and t1.create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59') t1 " +
         "    where  create_on BETWEEN '" + startTime + "' and '" + endTime + " 23:59:59' and row_num=1  and fwbh not in " +
         " (select fwbh from " + table_fwzt + " where final_fwzt IS NOT NULL AND final_create_on = '" + endTime + " 23:59:59') " + strWhere + ")  order by village ) t where  1=1  " + strWhere ;

            }



            if (sql_count.Contains("insert into", StringComparison.OrdinalIgnoreCase)
                || sql_count.Contains("delete from", StringComparison.OrdinalIgnoreCase)
                || sql_count.Contains("update ", StringComparison.OrdinalIgnoreCase))
            {
                return new RespRet<Page<object>>(false, "不符合规范，禁止执行");
            }
            //执行语句
            var list = _dbContext.Database.GetDbConnection().Query<object>(sql).ToList();
            int count = _dbContext.Database.GetDbConnection().QueryFirstOrDefault<int>(sql_count);

            return new RespRet<Page<object>>(true, "ok", new Page<object> { Items = list, NextPageIndex = pageIndex, PageTotal = count, Total = count });

        }
    }

    public class Result 
    {
        public bool code;
        public string result;
    }
}




